bool ReceiveDBFile(int sockfd) { std::string tmp_file = "db_transfer.tmp"; FILE *fh = fopen(filename.c_str(), "wb"); long partition_id_long = readfile(sockfd); fclose(fh); if (partition_id_long == kReadFileFailure) { return false; } partition_t partition_id = (partition_t) partition_id_long; std::string partition_filename = GetPartitionDBFilename(partition_id); g_current_node_state.partition_map_lock.acquireWRLock(); // 1 assert(g_current_node_state.partition_map.find(partition_id) != g_current_node_state.partition_map.end()); PartitionMetadata pm = g_current_node_state.partition_map[partition_id]; if (pm.state == RECEIVING) { rename(tmp_file.c_str(), partition_filename); pm.db = new PartitionDB(partition_filename); g_current_node_state.state = RECEIVED; g_current_node_state.partition_map[partition_id] = pm; g_current_node_state.savePartitionState[g_owned_partition_state_filename]; } g_current_node_state.partition_map_lock.releaseWRLock(); // 1 return true; }
static void InitializeState() { g_current_node_state = new NodeStateMachine(); char hostname[HOST_NAME_MAX + 1]; if (gethostname(hostname, sizeof(hostname)) != 0) { perror("Unable to gethostname for current machine. Exiting."); exit(1); } // print current machine's hostname and id std::cout << "Machine hostname : " << hostname << std::endl; g_current_node_id = hostToNodeId(std::string(hostname)); // from hash.h std::cout << "Machine node id : " << g_current_node_id << std::endl; // database direcory must not exist struct stat stat_database_dir;; if (lstat(g_db_files_dirname.c_str(), &stat_database_dir) == -1) { std::cout << "DB directory does not exist. Creating it" << std::endl; mkdir(g_db_files_dirname.c_str(), 0700); } else { std::cerr << "DB directory already exists. Perhaps you want to start the machine in recover mode? Fatal error." << std::endl; exit(1); } // cluster member list must exist <---------need boost struct stat stat_cluster_member_list; if (lstat(g_cluster_member_list_filename.c_str(), &stat_cluster_member_list) == -1) { std::cerr << "Could not stat cluster member list file. Fatal error." << std::endl; exit(1); } g_current_node_state->loadClusterMemberList(g_cluster_member_list_filename); // the intial partition map must exist <---------need boost struct stat stat_partition_table_file; if (lstat(g_cached_partition_map_filename.c_str(), &stat_partition_table_file) == -1) { std::cerr << "Could not stat partition-node map file. Fatal error." << std::endl; exit(1); } g_cached_partition_table = new PartitionTable(g_cached_partition_map_filename); // initialize partition table int num_partitions = g_cached_partition_table->getNumPartitions(); int num_replicas = g_cached_partition_table->getNumReplicas(); node_t *partition_table = g_cached_partition_table->getPartitionTable(); std::cout << "initialize for " << num_partitions << " partitions and " << num_replicas << " replicas" << std::endl; // build list of partitions owned from scratch for (partition_t partition_id = 0; partition_id < num_partitions; partition_id++) { int base_index = partition_id * num_replicas; for (int i = 0; i < num_replicas; i++) { if (partition_table[i + base_index] == g_current_node_id) { PartitionMetadata pm; pm.db = new PartitionDB(GetPartitionDBFilename(partition_id)); pm.state = PartitionState::STABLE; pm.pinned = false; g_current_node_state->partitions_owned_map[partition_id] = pm; } } } g_current_node_state->state = NodeState::STABLE; // save the partition state g_current_node_state->savePartitionState(g_owned_partition_state_filename); g_current_node_state->saveNodeState(g_current_node_state_filename); }