diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index ad781d0c..d7690894 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -1661,6 +1661,24 @@ jobs: echo "" done + test_layer_pools: + runs-on: ubuntu-latest + needs: build + container: ${{env.TEST_IMAGE}}:${{github.sha}} + steps: + - name: Run test + id: test + timeout-minutes: 3 + run: /root/vitastor/tests/test_layer_pools.sh + - name: Print logs + if: always() && steps.test.outcome == 'failure' + run: | + for i in /root/vitastor/testdata/*.log /root/vitastor/testdata/*.txt; do + echo "-------- $i --------" + cat $i + echo "" + done + test_ls_tree: runs-on: ubuntu-latest needs: build diff --git a/docs/config/pool.en.md b/docs/config/pool.en.md index d549ced1..34c9cd77 100644 --- a/docs/config/pool.en.md +++ b/docs/config/pool.en.md @@ -343,7 +343,9 @@ Read more about this parameter in [Cluster-Wide Disk Layout Parameters](layout-c "Sector" size of virtual disks in this pool. The value from /vitastor/config/global is used when unspecified. Similarly to block_size, only OSDs with matching -bitmap_granularity are used for each pool. +bitmap_granularity are used for each pool. All layers of an image chain (snapshots, +clones and their parents) must be in pools with the same bitmap_granularity, see +[create](../usage/cli.en.md#create). Read more about this parameter in [Cluster-Wide Disk Layout Parameters](layout-cluster.en.md#bitmap_granularity). diff --git a/docs/config/pool.ru.md b/docs/config/pool.ru.md index fa229c61..e6fd9409 100644 --- a/docs/config/pool.ru.md +++ b/docs/config/pool.ru.md @@ -344,6 +344,8 @@ meow недоступен". Размер "сектора" виртуальных дисков в данном пуле. Если не задан, используется значение из /vitastor/config/global. Аналогично block_size, каждый пул будет использовать только OSD с совпадающей с пулом настройкой bitmap_granularity. +Все слои одной цепочки образа (снимки, клоны и их родители) должны лежать в пулах +с одинаковым bitmap_granularity, см. [create](../usage/cli.ru.md#create). О самом параметре читайте в разделе [Дисковые параметры уровня кластера](layout-cluster.ru.md#bitmap_granularity). diff --git a/docs/usage/cli.en.md b/docs/usage/cli.en.md index 8992be5d..e54d0ce2 100644 --- a/docs/usage/cli.en.md +++ b/docs/usage/cli.en.md @@ -147,6 +147,10 @@ Create an image. Options: * `--owner-group NAME` - Set image owner group. Users in this group get full access to the image. * `--reader-group NAME` - Set image reader group. Users in this group get read-only access to the image. +All layers of a chain (an image, its snapshots and parents) must be in pools with the same +`bitmap_granularity`. `create --parent` and `snap-create -p` fail if they would chain layers +across such pools, and `flatten`, `merge-data` and `rm` refuse to merge layers of such a chain. + ``` vitastor-cli create --snapshot [OPTIONS] vitastor-cli snap-create [OPTIONS] @ diff --git a/docs/usage/cli.ru.md b/docs/usage/cli.ru.md index 7f0ed964..6cbdbbfc 100644 --- a/docs/usage/cli.ru.md +++ b/docs/usage/cli.ru.md @@ -151,6 +151,11 @@ bench-kaveri kaveri 10 G 10 G 0 B/s 0 0 0 us 0 B/s 0 * `--reader-group NAME` - Задать группу читателей образа. Пользователи из этой группы получают доступ к образу только на чтение. +Все слои одной цепочки (образ, его снимки и родители) должны лежать в пулах с одинаковым +`bitmap_granularity`. `create --parent` и `snap-create -p` завершаются с ошибкой, если получится +цепочка из слоёв в пулах с разным `bitmap_granularity`, а `flatten`, `merge-data` и `rm` +отказываются сливать слои такой цепочки. + ``` vitastor-cli create --snapshot [ОПЦИИ] vitastor-cli snap-create [ОПЦИИ] @ diff --git a/src/client/cluster_client.cpp b/src/client/cluster_client.cpp index c5eb1d8c..f422a8f7 100644 --- a/src/client/cluster_client.cpp +++ b/src/client/cluster_client.cpp @@ -1297,7 +1297,14 @@ resume_2: next_inode = icache->other_pool_parent_id; } } - if (next_inode) + int res = next_inode ? check_parent_layer(op, next_inode) : 1; + if (res < 0) + { + op->retval = res; + erase_op(op); + return 1; + } + if (res == 0) { // Continue reading from the parent inode icache = inode_cache_get(next_inode); @@ -1819,3 +1826,48 @@ void cluster_client_t::copy_part_bitmap(cluster_op_t *op, cluster_op_part_t *par part_len--; } } + +// Returns 0 if the read may continue into , 1 if it is already fully covered +// and -EINVAL if uses a different bitmap granularity and the read is partially +// covered or not aligned to the parent's granularity +int cluster_client_t::check_parent_layer(cluster_op_t *op, inode_t parent_id) +{ + auto cur_pool_it = st_cli->pool_config.find(INODE_POOL(op->cur_inode)); + auto parent_pool_it = st_cli->pool_config.find(INODE_POOL(parent_id)); + if (cur_pool_it == st_cli->pool_config.end() || parent_pool_it == st_cli->pool_config.end()) + { + return 0; + } + uint32_t gran = cur_pool_it->second.bitmap_granularity; + uint32_t parent_gran = parent_pool_it->second.bitmap_granularity; + if (gran == parent_gran) + { + return 0; + } + // op->bitmap_buf is indexed in units of the current layer's granularity + uint64_t bits = op->len / gran, set = 0; + for (uint64_t i = 0; i < bits; i++) + { + if (((uint8_t*)op->bitmap_buf)[i >> 3] & (1 << (i & 0x7))) + set++; + } + if (set == bits) + { + return 1; + } + if (!set && op->opcode == OSD_OP_READ && !(op->offset % parent_gran) && !(op->len % parent_gran)) + { + // Nothing to merge: slice_rw() zeroes the bitmap again in the parent's granularity + op->bitmap_buf_size = 0; + return 0; + } + if (granularity_mismatch_logged.insert(std::make_pair(op->cur_inode, parent_id)).second) + { + fprintf( + stderr, "Inode %ju in pool %u has parent %ju in pool %u with a different bitmap_granularity (%u vs %u), reads through it fail\n", + INODE_NO_POOL(op->cur_inode), INODE_POOL(op->cur_inode), INODE_NO_POOL(parent_id), INODE_POOL(parent_id), + gran, parent_gran + ); + } + return -EINVAL; +} diff --git a/src/client/cluster_client.h b/src/client/cluster_client.h index 857c7a23..e680e356 100644 --- a/src/client/cluster_client.h +++ b/src/client/cluster_client.h @@ -155,6 +155,7 @@ class __attribute__((visibility("default"))) cluster_client_t writeback_cache_t *wb = NULL; std::set dirty_osds; uint64_t dirty_bytes = 0, dirty_ops = 0; + std::set> granularity_mismatch_logged; // inodes require some extra state for read/write, it's stored here. // moreover, robin_hood access is slightly faster than std::map :) @@ -239,6 +240,7 @@ protected: void send_sync(cluster_op_t *op, cluster_op_part_t *part); void handle_op_part(cluster_op_part_t *part); void copy_part_bitmap(cluster_op_t *op, cluster_op_part_t *part); + int check_parent_layer(cluster_op_t *op, inode_t parent_id); void erase_op(cluster_op_t *op); void calc_wait(cluster_op_t *op); void inc_wait(uint64_t opcode, uint64_t flags, cluster_op_t *next, int inc); diff --git a/src/cmd/cli.cpp b/src/cmd/cli.cpp index 9186bd98..064eea4f 100644 --- a/src/cmd/cli.cpp +++ b/src/cmd/cli.cpp @@ -46,6 +46,7 @@ static const char* help_text = " -p|--pool POOL Specify pool for the new image (may be omitted if there is only 1 pool).\n" " --parent PARENT Create a copy-on-write image clone based on PARENT (or PARENT@SNAPSHOT).\n" " If parent is not a snapshot, it must be a read-only image.\n" + " All layers of a chain must be in pools with the same bitmap_granularity.\n" " --enc-key random Generate a new random AES-256-XTS encryption key for the new image.\n" " --enc-key HEX Set a specified AES-256-XTS key (64 bytes in hex) for the new image.\n" " --enc-key vault:ID Use an encryption key from an external Vault secret with specified ID.\n" diff --git a/src/cmd/cli.h b/src/cmd/cli.h index 200b6530..42c1ac50 100644 --- a/src/cmd/cli.h +++ b/src/cmd/cli.h @@ -58,6 +58,7 @@ public: json11::Json::object format_image(const inode_config_t & cfg); void change_parent(inode_t cur, inode_t old_parent, inode_t new_parent, cli_result_t *result); inode_config_t* get_inode_cfg(const std::string & name); + std::string check_chain_granularity(pool_id_t pool_id, inode_t layer); bool check_image_perm(const inode_config_t & cfg, bool write); diff --git a/src/cmd/cli_common.cpp b/src/cmd/cli_common.cpp index b91a41ec..d606e479 100644 --- a/src/cmd/cli_common.cpp +++ b/src/cmd/cli_common.cpp @@ -174,6 +174,37 @@ inode_config_t* cli_tool_t::get_inode_cfg(const std::string & name) return NULL; } +// Check that and all its parents are in pools with the same bitmap_granularity as +std::string cli_tool_t::check_chain_granularity(pool_id_t pool_id, inode_t layer) +{ + auto & pools = cli->st_cli->pool_config; + auto & inodes = cli->st_cli->inode_config; + auto pool_it = pools.find(pool_id); + if (pool_it == pools.end()) + { + return ""; + } + for (size_t i = 0; layer && i <= inodes.size(); i++) + { + auto ino_it = inodes.find(layer); + auto layer_pool_it = pools.find(INODE_POOL(layer)); + if (layer_pool_it != pools.end() && + layer_pool_it->second.bitmap_granularity != pool_it->second.bitmap_granularity) + { + return "Layers can't be chained across pools with different bitmap_granularity: pool "+ + pool_it->second.name+" has "+std::to_string(pool_it->second.bitmap_granularity)+", pool "+ + layer_pool_it->second.name+" (layer "+(ino_it != inodes.end() ? ino_it->second.name : std::to_string(INODE_NO_POOL(layer)))+ + ") has "+std::to_string(layer_pool_it->second.bitmap_granularity); + } + if (ino_it == inodes.end()) + { + break; + } + layer = ino_it->second.parent_id; + } + return ""; +} + void cli_tool_t::parse_api_opts(json11::Json::object & cfg) { iodepth = cfg["iodepth"].uint64_value(); diff --git a/src/cmd/cli_create.cpp b/src/cmd/cli_create.cpp index bf9fbc27..dddb789f 100644 --- a/src/cmd/cli_create.cpp +++ b/src/cmd/cli_create.cpp @@ -201,6 +201,13 @@ struct image_creator_t state = 100; return; } + std::string gran_err = parent->check_chain_granularity(new_pool_id, new_parent_id); + if (gran_err != "") + { + result = (cli_result_t){ .err = EINVAL, .text = gran_err }; + state = 100; + return; + } } if (!size && !force_size) { @@ -323,6 +330,15 @@ resume_3: { return; } + { + std::string gran_err = parent->check_chain_granularity(new_pool_id, INODE_WITH_POOL(old_pool_id, old_id)); + if (gran_err != "") + { + result = (cli_result_t){ .err = EINVAL, .text = gran_err }; + state = 100; + return; + } + } parent->etcd_txn(json11::Json::object { { "success", json11::Json::array { get_next_id() } } }); diff --git a/src/cmd/cli_merge.cpp b/src/cmd/cli_merge.cpp index 14f24595..6eadbb94 100644 --- a/src/cmd/cli_merge.cpp +++ b/src/cmd/cli_merge.cpp @@ -138,6 +138,13 @@ struct snap_merger_t } chain_list.push_back(from_cfg->num); layer_block_size[from_cfg->num] = get_block_size(from_cfg->num, NULL); + std::string gran_err = parent->check_chain_granularity(INODE_POOL(to_num), to_num); + if (gran_err != "") + { + result = (cli_result_t){ .err = EINVAL, .text = gran_err }; + state = 100; + return; + } int i = chain_list.size()-1; for (inode_t item: chain_list) { diff --git a/src/cmd/cli_rm.cpp b/src/cmd/cli_rm.cpp index 48d4d8cf..17e6da06 100644 --- a/src/cmd/cli_rm.cpp +++ b/src/cmd/cli_rm.cpp @@ -122,6 +122,9 @@ struct snap_remover_t } // Find all images to delete get_rm_ranks(); + if (state == 100) + return; + check_children_granularity(); if (state == 100) return; // Mark images as deleted to prevent rebasing newer images on them @@ -370,6 +373,25 @@ resume_100: } } + // Before mark_deleted(): a merge refused later would leave the layers marked deleted + void check_children_granularity() + { + for (auto & ic: parent->cli->st_cli->inode_config) + { + if (ic.second.parent_id && sources.find(ic.second.parent_id) != sources.end() && + sources.find(ic.second.num) == sources.end()) + { + std::string err = parent->check_chain_granularity(INODE_POOL(ic.second.num), ic.second.num); + if (err != "") + { + result = (cli_result_t){ .err = EINVAL, .text = err }; + state = 100; + return; + } + } + } + } + void get_merge_children() { // Get all children of from..to diff --git a/src/test/test_cluster_client.cpp b/src/test/test_cluster_client.cpp index 7a941b18..6e156193 100644 --- a/src/test/test_cluster_client.cpp +++ b/src/test/test_cluster_client.cpp @@ -1342,6 +1342,116 @@ void test_sync_fail_dirty_state_loss() printf("[ok] sync fail dirty state loss test\n"); } +void test_parent_granularity_mismatch(uint64_t child_gran, uint64_t parent_gran) +{ + json11::Json config; + timerfd_manager_t *tfd = new timerfd_manager_t([](int fd, bool wr, std::function callback){}); + etcd_mock_t etcd_store; + etcd_state_client_mock_t *mock = new etcd_state_client_mock_t(etcd_store); + mock->pause(); + cluster_client_t *cli = new cluster_client_t(NULL, tfd, config, std::unique_ptr(mock)); + + configure_single_pg_pool(mock); + mock->set("/vitastor/config/pools", json11::Json::object { + { "1", json11::Json::object { + { "name", "hddpool" }, + { "scheme", "replicated" }, + { "pg_size", 2 }, + { "pg_minsize", 1 }, + { "pg_count", 1 }, + { "failure_domain", "osd" }, + { "immediate_commit", "none" }, + { "bitmap_granularity", child_gran }, + } }, + { "2", json11::Json::object { + { "name", "ssdpool" }, + { "scheme", "replicated" }, + { "pg_size", 1 }, + { "pg_minsize", 1 }, + { "pg_count", 1 }, + { "failure_domain", "osd" }, + { "immediate_commit", "none" }, + { "bitmap_granularity", parent_gran }, + } }, + }); + mock->set("/vitastor/pg/config", json11::Json::object { + { "items", json11::Json::object { + { "1", json11::Json::object { + { "1", json11::Json::object { + { "osd_set", json11::Json::array { 1, 2 } }, + { "primary", 1 }, + } } + } }, + { "2", json11::Json::object { + { "1", json11::Json::object { + { "osd_set", json11::Json::array { 2 } }, + { "primary", 2 }, + } } + } }, + } } + }); + mock->set("/vitastor/pg/state/2/1", json11::Json::object { + { "peers", json11::Json::array { 2 } }, + { "primary", 2 }, + { "state", json11::Json::array { "active" } }, + }); + mock->set("/vitastor/config/inode/1/1", json11::Json::object { + { "name", "child" }, + { "parent_id", 1 }, + { "parent_pool", 2 }, + }); + mock->set("/vitastor/config/inode/2/1", json11::Json::object { + { "name", "parent" }, + }); + mock->resume(); + pretend_connected(cli, 1); + pretend_connected(cli, 2); + + // Fully written in the child layer: the parent isn't needed + int *r1 = test_read(cli, 0, 4096); + osd_op_t *op = find_op(cli, 1, OSD_OP_READ, 0, 4096); + memset(op->bitmap, 0xff, op->bitmap_len); + can_complete(r1); + pretend_op_completed(cli, op, 0); + check_completed(r1); + check_op_count(cli, 2, 0); + + // Not written in the child layer: the whole range is read from the parent + int *r2 = test_read(cli, 0, 4096); + pretend_op_completed(cli, find_op(cli, 1, OSD_OP_READ, 0, 4096), 0); + check_op_count(cli, 2, 1); + op = cli->msgr->osd_peers.at(2)->sent_ops.begin()->second; + assert(op->req.rw.inode == 0x2000000000001 && op->req.rw.offset == 0 && op->req.rw.len == 4096); + can_complete(r2); + pretend_op_completed(cli, op, 0); + check_completed(r2); + + // Partly written in the child layer: the bitmaps can't be merged + int *r3 = test_read(cli, 0, 8192); + op = find_op(cli, 1, OSD_OP_READ, 0, 8192); + ((uint8_t*)op->bitmap)[0] = 1; + can_complete(r3); + pretend_op_completed(cli, op, 0); + assert(*r3 == -EINVAL); + delete r3; + check_op_count(cli, 2, 0); + + if (child_gran < parent_gran) + { + // Not written in the child layer, but not aligned to the parent's granularity + int *r4 = test_read(cli, 0, child_gran); + can_complete(r4); + pretend_op_completed(cli, find_op(cli, 1, OSD_OP_READ, 0, child_gran), 0); + assert(*r4 == -EINVAL); + delete r4; + check_op_count(cli, 2, 0); + } + + delete cli; + delete tfd; + printf("[ok] test_parent_granularity_mismatch %ju over %ju\n", child_gran, parent_gran); +} + int main(int narg, char *args[]) { @@ -1359,5 +1469,7 @@ int main(int narg, char *args[]) test_pg_config_disappears(); test_sync_released_during_continue_ops(); test_snapshot_over_nonexistent_pool(); + test_parent_granularity_mismatch(4096, 512); + test_parent_granularity_mismatch(512, 4096); return 0; } diff --git a/tests/run_tests.sh b/tests/run_tests.sh index caac41b7..7088d8fa 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -131,6 +131,7 @@ OLD=1 ./test_corrupt_all.sh ./test_reweight_half.sh ./test_snapshot_pool2.sh +./test_layer_pools.sh ./test_ls_tree.sh ./test_snapshot_read_bitmap.sh diff --git a/tests/test_layer_pools.sh b/tests/test_layer_pools.sh new file mode 100755 index 00000000..7c9e6919 --- /dev/null +++ b/tests/test_layer_pools.sh @@ -0,0 +1,95 @@ +#!/bin/bash -ex + +. `dirname $0`/run_3osds.sh + +# Layer chains across pools + +for i in 4 5 6; do + OSD_ARGS="$OSD_ARGS --block_size 1048576" start_osd $i +done + +$ETCDCTL put /vitastor/config/pools '{"1":{'$POOLCFG',"pg_size":'$PG_SIZE',"pg_minsize":'$PG_MINSIZE',"pg_count":'$PG_COUNT'},'\ +'"2":{"name":"samepool","scheme":"replicated","failure_domain":"osd","pg_size":2,"pg_minsize":2,"pg_count":1},'\ +'"3":{"name":"finepool","scheme":"replicated","failure_domain":"osd","pg_size":2,"pg_minsize":2,"pg_count":1,"bitmap_granularity":512},'\ +'"4":{"name":"ecpool","scheme":"ec","parity_chunks":1,"failure_domain":"osd","pg_size":3,"pg_minsize":2,"pg_count":1},'\ +'"5":{"name":"bigpool","scheme":"replicated","failure_domain":"osd","pg_size":2,"pg_minsize":2,"pg_count":1,"block_size":1048576}}' + +wait_pool_up 60 2 2 1 +wait_pool_up 60 4 3 1 +wait_pool_up 60 5 2 1 + +images() +{ + $VITASTOR_CLI ls --json | jq -r '[ .[].name ] | sort | join(" ")' +} + +$VITASTOR_CLI create -s 128M img -p testpool + +# Pools with different bitmap_granularity can't be chained + +if $VITASTOR_CLI snap-create img@s1 -p finepool; then + format_error "Snapshot into a pool with a different bitmap_granularity must be refused" +fi +[[ "$(images)" == "img" ]] || format_error "Refused snapshot changed images: $(images)" + +$VITASTOR_CLI snap-create img@s0 +if $VITASTOR_CLI create --parent img@s0 -p finepool clone; then + format_error "Clone into a pool with a different bitmap_granularity must be refused" +fi +[[ "$(images)" == "img img@s0" ]] || format_error "Refused clone changed images: $(images)" + +$VITASTOR_CLI snap-create img@s2 -p samepool +$VITASTOR_CLI create --parent img@s0 -p samepool clone +[[ "$(images)" == "clone img img@s0 img@s2" ]] || format_error "Unexpected images: $(images)" + +# Merging a chain that mixes granularities fails before writing anything + +$ETCDCTL put /vitastor/config/inode/3/1 '{"name":"fineparent","size":4194304,"readonly":true}' +$ETCDCTL put /vitastor/config/inode/1/100 '{"name":"mixed","size":4194304,"parent_id":1,"parent_pool":3}' + +$VITASTOR_FIO -bs=1M -direct=1 -iodepth=4 -rw=write -size=4M -image=mixed -mirror_file=./testdata/bin/mirror.bin -randrepeat=0 + +if $VITASTOR_CLI flatten mixed >./testdata/flatten.log 2>&1; then + format_error "Flatten of a chain with mixed bitmap_granularity must fail" +fi +grep -q bitmap_granularity ./testdata/flatten.log || format_error "Flatten failed for another reason: $(cat ./testdata/flatten.log)" +[[ "$($VITASTOR_CLI ls --json | jq -r '.[] | select(.name == "mixed") | .parent_name')" == "fineparent" ]] || + format_error "Failed flatten changed the parent of mixed" +if $VITASTOR_CLI rm fineparent >./testdata/rm.log 2>&1; then + format_error "rm that merges a chain with mixed bitmap_granularity must fail" +fi +grep -q bitmap_granularity ./testdata/rm.log || format_error "rm failed for another reason: $(cat ./testdata/rm.log)" +[[ "$($VITASTOR_CLI ls --json | jq -r '.[] | select(.name == "fineparent") | .deleted')" == "false" ]] || + format_error "Refused rm marked fineparent deleted" +$VITASTOR_CLI dd iimg=mixed of=./testdata/bin/res.bin bs=1M iodepth=4 +cmp ./testdata/bin/res.bin ./testdata/bin/mirror.bin + +# Chains across pools with different object sizes + +check_image() +{ + $VITASTOR_CLI dd iimg=$1 of=./testdata/bin/res.bin bs=1M iodepth=4 + cmp ./testdata/bin/res.bin ./testdata/bin/$1.bin +} + +check_chain() +{ + local img=$1 from=$2 to=$3 + $VITASTOR_CLI create -s 16M $img -p $from + $VITASTOR_FIO -bs=1M -direct=1 -iodepth=4 -rw=write -size=16M -image=$img -mirror_file=./testdata/bin/$img.bin -randrepeat=0 + $VITASTOR_CLI snap-create $img@mig -p $to + $VITASTOR_FIO -bs=4k -direct=1 -iodepth=4 -rw=randwrite -size=16M -number_ios=256 -image=$img \ + -mirror_file=./testdata/bin/$img.bin -randrepeat=0 + check_image $img + $VITASTOR_CLI flatten $img + check_image $img + $VITASTOR_CLI rm $img@mig + check_image $img +} + +check_chain big2small bigpool testpool +check_chain small2big testpool bigpool +check_chain ec2rep ecpool testpool +check_chain rep2ec testpool ecpool + +format_green OK