map_with_crush():
1 void map_with_crush(int replication_count, int hosts_count, int object_map[][NUMBER_OF_OBJECTS]) { 2 struct crush_map *m = crush_create(); 3 m->choose_local_tries = 0; 4 m->choose_local_fallback_tries = 0; 5 m->choose_total_tries = 50; 6 m->chooseleaf_descend_once = 1; 7 m->chooseleaf_vary_r = 1; 8 m->chooseleaf_stable = 1; 9 m->allowed_bucket_algs = 10 (1 << CRUSH_BUCKET_UNIFORM) | 11 (1 << CRUSH_BUCKET_LIST) | 12 (1 << CRUSH_BUCKET_STRAW2); 13 int root_type = 1; 14 int host_type = 2; 15 int bucketno = 0; 16 17 int hosts[hosts_count]; 18 int weights[hosts_count]; 19 int disk = 0; 20 int weight = 0x10000; // 1.0 21 // 创建一个多个host类型的bucket,并给每个bucket添加一个disk的item 22 for(int host = 0; host < hosts_count; host++) { 23 struct crush_bucket *b; 24 // 函数创建一个bucket结构。需要指定CRUSH算法(uniform、list、tree、straw)、hash算法、 25 // bucket类型(自定义的type)、size(包括多少个item)、这些items的id数组、这些items的weights 26 b = crush_make_bucket(m, CRUSH_BUCKET_STRAW2, CRUSH_HASH_DEFAULT, host_type, 27 0, NULL, NULL); 28 assert(b != NULL); 29 // 往bucket中添加item, disk为item的id,weight为item的权重 30 assert(crush_bucket_add_item(m, b, disk, weight) == 0); 31 // 将b添加到m中,m->buckets[],bucket的id为bucketno 32 assert(crush_add_bucket(m, 0, b, &bucketno) == 0); 33 hosts[host] = bucketno; 34 weights[host] = weight; 35 disk++; 36 } 37 38 struct crush_bucket *root; 39 int bucket_root; 40 // 创建一个root类型的bucket,bucket中添加的item是hosts 41 root = crush_make_bucket(m, CRUSH_BUCKET_STRAW2, CRUSH_HASH_DEFAULT, root_type, 42 hosts_count, hosts, weights); 43 assert(root != NULL); 44 // 将bucket添加带map中 45 assert(crush_add_bucket(m, 0, root, &bucket_root) == 0); 46 // 调整权重分布 47 assert(crush_reweight_bucket(m, root) == 0); 48 49 struct crush_rule *r; 50 int minsize = 1; 51 int maxsize = 5; 52 int number_of_steps = 3; 53 r = crush_make_rule(number_of_steps, 0, 0, minsize, maxsize); 54 assert(r != NULL); 55 crush_rule_set_step(r, 0, CRUSH_RULE_TAKE, bucket_root, 0); 56 crush_rule_set_step(r, 1, CRUSH_RULE_CHOOSELEAF_FIRSTN, replication_count, host_type); 57 crush_rule_set_step(r, 2, CRUSH_RULE_EMIT, 0, 0); 58 int ruleno = crush_add_rule(m, r, -1); 59 assert(ruleno >= 0); 60 61 crush_finalize(m); 62 63 { 64 int result[replication_count]; 65 __u32 weights[hosts_count]; 66 for(int i = 0; i < hosts_count; i++) 67 weights[i] = 0x10000; 68 int cwin_size = crush_work_size(m, replication_count); 69 char cwin[cwin_size]; 70 crush_init_workspace(m, cwin); 71 for(int x = 0; x < NUMBER_OF_OBJECTS; x++) { 72 memset(result, '