bool ExpandableBlockStreamExchangeEpoll::open(const PartitionOffset& partition_offset) { unsigned long long int start = curtick(); RegisterExpandedThreadToAllBarriers(); if (tryEntryIntoSerializedSection()) { debug_winner_thread++; nexhausted_lowers=0; this->partition_offset=partition_offset; nlowers=state.lower_id_list_.size(); for (unsigned i = 0; i < nlowers; i++) { debug_received_block[i] = 0; } socket_fd_lower_list = new int[nlowers]; //init -1 ---Yu for (int i = 0; i < nlowers; ++i) { socket_fd_lower_list[i] = -1; } buffer=new BlockStreamBuffer(state.block_size_,BUFFER_SIZE_IN_EXCHANGE,state.schema_); ExpanderTracker::getInstance()->addNewStageEndpoint(pthread_self(),LocalStageEndPoint(stage_src,"Exchange",buffer)); received_block_stream_=BlockStreamBase::createBlock(state.schema_,state.block_size_); block_for_socket_ = new BlockContainer*[nlowers]; for (unsigned i = 0; i < nlowers; i++) { block_for_socket_[i] = new BlockContainer(received_block_stream_->getSerializedBlockSize()); } if (PrepareTheSocket() == false) return false; if (SetSocketNonBlocking(sock_fd) == false) { return false; } logging_->log("[%ld,%d] Open: nexhausted lowers=%d, nlower=%d", state.exchange_id_, partition_offset, nexhausted_lowers, nlowers); if (RegisterExchange() == false) { logging_->elog("Register Exchange with ID=%d fails!", state.exchange_id_); } if(isMaster()){ /* According to a bug reported by dsc, the master exchangeupper should check whether other * uppers have registered to exchangeTracker. Otherwise, the lower may fail to connect to the * exchangeTracker of some uppers when the lower nodes receive the exchagnelower, as some uppers * have not register the exchange_id to the exchangeTracker. */ logging_->log("[%ld,%d] Synchronizing....", state.exchange_id_, partition_offset); checkOtherUpperRegistered(); logging_->log("[%ld,%d] Synchronized!", state.exchange_id_, partition_offset); logging_->log("[%ld,%d] This exchange is the master one, serialize the iterator subtree to the children...", state.exchange_id_, partition_offset); if (SerializeAndSendToMulti() == false) return false; } if (CreateReceiverThread() == false) { return false; } createPerformanceInfo(); } /* A synchronization barrier, in case of multiple expanded threads*/ barrierArrive(); return true; }
bool EClientSocket::eConnectImpl(int clientId, bool extraAuth, ConnState* stateOutPt) { // resolve host struct hostent* hostEnt = gethostbyname( host().c_str()); if ( !hostEnt) { getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg()); return false; } // create socket m_fd = socket(AF_INET, SOCK_STREAM, 0); // cannot create socket if( m_fd < 0) { getWrapper()->error( NO_VALID_ID, FAIL_CREATE_SOCK.code(), FAIL_CREATE_SOCK.msg()); return false; } // starting to connect to server struct sockaddr_in sa; memset( &sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons( port()); sa.sin_addr.s_addr = ((in_addr*)hostEnt->h_addr)->s_addr; // try to connect if( (connect( m_fd, (struct sockaddr *) &sa, sizeof( sa))) < 0) { // error connecting SocketClose( m_fd); m_fd = -1; getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg()); return false; } getTransport()->fd(m_fd); // set client id setClientId( clientId); setExtraAuth( extraAuth); int res = sendConnectRequest(); if (res < 0 && !handleSocketError()) return false; if( !isConnected()) { if( connState() != CS_DISCONNECTED) { assert( connState() == CS_REDIRECT); if( stateOutPt) { *stateOutPt = connState(); } eDisconnect(); } return false; } // set socket to non-blocking state if ( !SetSocketNonBlocking(m_fd)) { // error setting socket to non-blocking eDisconnect(); getWrapper()->error( NO_VALID_ID, CONNECT_FAIL.code(), CONNECT_FAIL.msg()); return false; } assert( connState() == CS_CONNECTED); if( stateOutPt) { *stateOutPt = connState(); } if (!m_asyncEConnect) { EReader reader(this, m_pSignal); while (m_pSignal && !m_serverVersion && isSocketOK()) { reader.checkClient(); m_pSignal->waitForSignal(); reader.processMsgs(); } } // successfully connected return isSocketOK(); }
/** * note the serialized block's size is different from others, it has tail info. * exchange merger is at the end of one segment of plan, so it's the "stage_src" * for this stage */ bool ExchangeMerger::Open(const PartitionOffset& partition_offset) { unsigned long long int start = curtick(); RegisterExpandedThreadToAllBarriers(); if (TryEntryIntoSerializedSection()) { // first arrived thread dose exhausted_lowers = 0; this->partition_offset_ = partition_offset; lower_num_ = state_.lower_id_list_.size(); socket_fd_lower_list_ = new int[lower_num_]; for (int i = 0; i < lower_num_; ++i) { socket_fd_lower_list_[i] = -1; } // buffer all deserialized blocks come from every socket all_merged_block_buffer_ = new BlockStreamBuffer( state_.block_size_, BUFFER_SIZE_IN_EXCHANGE, state_.schema_); ExpanderTracker::getInstance()->addNewStageEndpoint( pthread_self(), LocalStageEndPoint(stage_src, "Exchange", all_merged_block_buffer_)); // if one of block_for_socket is full, it will be deserialized into // block_for_deserialization and sended to all_merged_data_buffer block_for_deserialization = BlockStreamBase::createBlock(state_.schema_, state_.block_size_); // store block for each socket and the received block is serialized. block_for_socket_ = new BlockContainer* [lower_num_]; for (unsigned i = 0; i < lower_num_; ++i) { block_for_socket_[i] = new BlockContainer( block_for_deserialization->getSerializedBlockSize()); } if (PrepareSocket() == false) return false; if (SetSocketNonBlocking(sock_fd_) == false) { return false; } LOG(INFO) << "exchange_id = " << state_.exchange_id_ << " partition_offset = " << partition_offset << " Open: exhausted lower senders num = " << exhausted_lowers << " lower sender num = " << lower_num_ << std::endl; if (RegisterExchange() == false) { LOG(ERROR) << "Register Exchange with ID = " << state_.exchange_id_ << " fails!" << std::endl; } if (IsMaster()) { /* According to a bug reported by dsc, the master exchange upper should * check whether other uppers have registered to exchangeTracker. * Otherwise, the lower may fail to connect to the exchangeTracker of some * uppers when the lower nodes receive the exchange lower, as some uppers * have not register the exchange_id to the exchangeTracker. */ LOG(INFO) << " exchange_id = " << state_.exchange_id_ << " partition_offset = " << partition_offset << "Synchronizing...." << std::endl; IsOtherMergersRegistered(); LOG(INFO) << " exchange_id = " << state_.exchange_id_ << " partition_offset = " << partition_offset << " Synchronized! Then serialize and send its next segment " "plan to all its lower senders" << std::endl; if (SerializeAndSendPlan() == false) return false; } if (CreateReceiverThread() == false) { return false; } CreatePerformanceInfo(); } /// A synchronization barrier, in case of multiple expanded threads BarrierArrive(); return true; }
int init_client (void) { long bytes, pport; int gx, gy; int retries = 0; /*Open server socket */ printf ("Opening server socket\n"); if ((socket_c = CreateDgramSocket (0)) == -1) { error ("Could not create connection socket"); SocketClose (socket_c); exit (-1); } strcpy(hostname,GetSockAddr(socket_c)); /*Create connection message */ strcpy ((char *) buffer, "Koules"); PUTCHAR (buffer + 7, VERSION); PUTSHORT (buffer + 8, GAMEWIDTH); PUTSHORT (buffer + 10, GAMEHEIGHT); PUTLONG (buffer + 12, (long) getpid ()); printf ("Asking server at %s port %i\n", servername, initport); #if 0 if (DgramConnect (socket_c, servername, initport) == -1) { perror ("Connection error"); error ("Can't connect to server %s on port %d", servername, port); close (socket_c); return -1; } #endif again: GetSocketError (socket_c); errno = 0; do { if (retries) sleep (1); retries++; if (retries > 100) printf (" Connection timed out\n"), exit (-1); if (errno) { perror ("Can't receive reply"); exit (-1); } if (DgramSend (socket_c, servername, initport, (char *) buffer, INITPACKETSIZE) == -1) { SocketClose (socket_c); perror ("Can't send message to server"); exit (-1); } if (errno) { perror ("Can't send message to server"); /*exit (-1); */ GetSocketError (socket_c); errno = 0; } SetTimeout (1, 10 * 1000); } while (!(SocketReadable (socket_c))); bytes = DgramReceiveAny (socket_c, (char *) buffer, BUFFERSIZE); if (bytes != REPLYSIZE) goto again; SocketClose (socket_c); GETLONG (buffer, port); if (port == 0) { printf ("Server refused me! (too many players or incompatible screen size)\n" "Try -W server's option is you are using 320x200 clients..\n"); exit (-1); } GETSHORT ((buffer + 4), gx); GETSHORT ((buffer + 6), gy); printf ("YYYYAAAAAAA Server replied.\n" "Opening port %i\n", port); if ((sock = CreateDgramSocket (0)) == -1) { error ("Can't create datagram socket"); return -1; } SetTimeout (1, 10 * 1000); /*if (DgramConnect (sock, servername, port) == -1) { error ("Can't connect to server %s on port %d", servername, port); close (sock); return -1; } */ if (SetSocketNonBlocking (sock, 1) == -1) { error ("Can't make socket non-blocking"); return -1; } printf ("Sending initialization message\n"); pport = GetPortNum (sock); if (gx / DIV > GAMEWIDTH || gy / DIV > GAMEHEIGHT) { printf ("Server's gamepool too large\n" "Try -W on server's command line\n"); PUTLONG (buffer, 0); if (DgramSend (sock, servername, port, (char *) buffer, 4) == -1) close (sock); exit (-1); } else GAMEWIDTH = gx, GAMEHEIGHT = gy; PUTHEAD (SINIT); PUTLONG (buffer + HEADSIZE, pport); if (SetSocketReceiveBufferSize (sock, NETBUFFER) == -1) { error ("Can't set socket buffer size"); return -1; } if (SetSocketSendBufferSize (sock, NETBUFFER) == -1) { error ("Can't set socket buffer size"); return -1; } GetSocketError (sock); csendreliable (buffer, 4 + HEADSIZE); csendbuffer (); csendbuffer (); csendbuffer (); printf ("Starting game...\n"); return (0); }