bool ExpandableBlockStreamExchangeEpoll::RegisterExchange() { ExchangeTracker* et = Environment::getInstance()->getExchangeTracker(); std::ostringstream port_str; port_str << socket_port; return et->RegisterExchange(ExchangeID(state.exchange_id_, partition_offset), port_str.str()); }
bool ExchangeMerger::RegisterExchange() { ExchangeTracker* et = Environment::getInstance()->getExchangeTracker(); std::ostringstream port_str; port_str << socket_port_; return et->RegisterExchange( ExchangeID(state_.exchange_id_, partition_offset_), port_str.str()); }
/** * ask the upper merger about the socket port information, and build socket * connection with it. the socket port information is maintain by * ExchangeTracker at corresponding Node. */ bool ExchangeSender::ConnectToUpper(const ExchangeID& exchange_id, const NodeID& id, int& sock_fd) const { // struct hostent* host; ExchangeTracker* et = Environment::getInstance()->getExchangeTracker(); int upper_port; NodeAddress upper_addr; if (!(et->AskForSocketConnectionInfo(exchange_id, id, upper_addr))) { LOG(ERROR) << "Fails to ask Node for socket connection info, the exchange id= " << exchange_id.exchange_id << std::endl; return false; } #ifdef CONNECTION_VERIFY stringstream ss; ss << "EXCHID" << exchange_id.exchange_id; string upper_passwd = ss.str(); int upper_passwd_len = upper_passwd.length(); #endif if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket creation errors!\n"); return false; } struct sockaddr_in serv_add; serv_add.sin_family = AF_INET; serv_add.sin_port = htons(atoi(upper_addr.port.c_str())); serv_add.sin_addr.s_addr = inet_addr(upper_addr.ip.c_str()); bzero(&(serv_add.sin_zero), 8); int returnvalue; if ((returnvalue = connect(sock_fd, (struct sockaddr*)&serv_add, sizeof(struct sockaddr))) == -1) { LOG(ERROR) << "Fails to connect remote socket: " << inet_ntoa(serv_add.sin_addr) << " , port= " << upper_addr.port << std::endl; return false; } LOG(INFO) << "exchid=" << exchange_id.exchange_id << "upper_offset=" << exchange_id.partition_offset << " connected to the upper socket.("<< upper_addr.ip.c_str() <<":" << upper_addr.port.c_str() << " sock_fd=" << sock_fd <<") return value:" << returnvalue << std::endl; #ifdef CONNECTION_VERIFY if ((returnvalue = send(sock_fd, upper_passwd.c_str(), upper_passwd_len, 0)) == -1 ) { LOG(ERROR) << "Failed to send acknowledgement to the upper socket. returnvalue:[" << returnvalue << "] errno:[" << errno << "]"; return false; } LOG(INFO) << "send acknowledgement to the upper socket: ("<< upper_passwd <<")" << std::endl; WaitingForNotification(sock_fd); #endif return true; }
/** * make sure each exchange merger at the same segment is registered, otherwise * result into bugs, one lower sender would fail to connect to one upper merger * due to it hasn't prepared. */ bool ExchangeMerger::IsOtherMergersRegistered() { ExchangeTracker* et = Environment::getInstance()->getExchangeTracker(); for (unsigned i = 0; i < state_.upper_id_list_.size(); i++) { NodeID id = state_.upper_id_list_[i]; /* Repeatedly ask node with ip for port information until the received port * is other than 0, which means * that the exchangeId on noede ip is registered to the exchangeTracker*/ int wait_time_in_millisecond = 1; NodeAddress node_addr; while (!et->AskForSocketConnectionInfo(ExchangeID(state_.exchange_id_, i), id, node_addr)) { usleep(wait_time_in_millisecond); wait_time_in_millisecond = wait_time_in_millisecond < 200 ? wait_time_in_millisecond + 20 : 200; } } }