예제 #1
0
  protected MessagingService() {
    for (ReservedVerbs_ verbs : ReservedVerbs_.values()) {
      reservedVerbs_.put(verbs.toString(), verbs.toString());
    }
    verbHandlers_ = new HashMap<String, IVerbHandler>();
    endPoints_ = new HashSet<EndPoint>();
    /*
     * Leave callbacks in the cachetable long enough that any related messages will arrive
     * before the callback is evicted from the table. The concurrency level is set at 128
     * which is the sum of the threads in the pool that adds shit into the table and the
     * pool that retrives the callback from here.
     */
    int maxSize = MessagingConfig.getMessagingThreadCount();
    callbackMap_ = new Cachetable<String, IAsyncCallback>(2 * DatabaseDescriptor.getRpcTimeout());
    taskCompletionMap_ =
        new Cachetable<String, IAsyncResult>(2 * DatabaseDescriptor.getRpcTimeout());

    messageDeserializationExecutor_ =
        new DebuggableThreadPoolExecutor(
            maxSize,
            maxSize,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGING-SERVICE-POOL"));

    messageSerializerExecutor_ =
        new DebuggableThreadPoolExecutor(
            maxSize,
            maxSize,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGE-SERIALIZER-POOL"));

    messageDeserializerExecutor_ =
        new DebuggableThreadPoolExecutor(
            maxSize,
            maxSize,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGE-DESERIALIZER-POOL"));

    streamExecutor_ =
        new DebuggableThreadPoolExecutor(
            1,
            1,
            Integer.MAX_VALUE,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            new ThreadFactoryImpl("MESSAGE-STREAMING-POOL"));

    protocol_ = hash(HashingSchemes.MD5, "FB-MESSAGING".getBytes());
    /* register the response verb handler */
    registerVerbHandlers(MessagingService.responseVerbHandler_, new ResponseVerbHandler());
    /* register stage for response */
    StageManager.registerStage(
        MessagingService.responseStage_, new MultiThreadedStage("RESPONSE-STAGE", maxSize));
  }
  public PaxosResponseType get() {
    long timeout = DatabaseDescriptor.getRpcTimeout() - (System.currentTimeMillis() - startTime);
    boolean success;
    try {
      success = condition.await(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ex) {
      throw new AssertionError(ex);
    }

    if (!success) {
      return PaxosResponseType.Timeout;
    }

    if (nackcount.get() >= expectedResponses) return PaxosResponseType.Nack;

    // normal paxos phase 2
    if (expectedValue != null) {
      //			System.out.println("AcceptResponseHandler : Quorum");
      return PaxosResponseType.Quorum;
    }
    // no-op phase 2
    else {
      for (Entry<IPaxosValue, AtomicInteger> entry : responseValues.entrySet()) {
        if (entry.getValue().get() >= expectedResponses) {
          this.acceptedValue = entry.getKey();
          return PaxosResponseType.Quorum;
        }
      }
      this.acceptedValue = null;
      return PaxosResponseType.NoConsensus;
    }
  }
    public void reduce(
        Text key, Iterator<Text> values, OutputCollector<Text, Text> output, Reporter reporter)
        throws IOException {
      ColumnFamily columnFamily;
      String keyspace = "Keyspace1";
      String cfName = "Super1";
      Message message;
      List<ColumnFamily> columnFamilies;
      columnFamilies = new LinkedList<ColumnFamily>();
      String line;

      /* Create a column family */
      columnFamily = ColumnFamily.create(keyspace, cfName);
      while (values.hasNext()) {
        // Split the value (line based on your own delimiter)
        line = values.next().toString();
        String[] fields = line.split("\1");
        String SuperColumnName = fields[1];
        String ColumnName = fields[2];
        String ColumnValue = fields[3];
        int timestamp = 0;
        columnFamily.addColumn(
            new QueryPath(
                cfName, ByteBufferUtil.bytes(SuperColumnName), ByteBufferUtil.bytes(ColumnName)),
            ByteBufferUtil.bytes(ColumnValue),
            timestamp);
      }

      columnFamilies.add(columnFamily);

      /* Get serialized message to send to cluster */
      message = createMessage(keyspace, key.getBytes(), cfName, columnFamilies);
      List<IAsyncResult> results = new ArrayList<IAsyncResult>();
      for (InetAddress endpoint :
          StorageService.instance.getNaturalEndpoints(keyspace, ByteBufferUtil.bytes(key))) {
        /* Send message to end point */
        results.add(MessagingService.instance().sendRR(message, endpoint));
      }
      /* wait for acks */
      for (IAsyncResult result : results) {
        try {
          result.get(DatabaseDescriptor.getRpcTimeout(), TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
          // you should probably add retry logic here
          throw new RuntimeException(e);
        }
      }

      output.collect(key, new Text(" inserted into Cassandra node(s)"));
    }
예제 #4
0
 private boolean connect() {
   if (logger.isDebugEnabled()) logger.debug("attempting to connect to " + endpoint);
   long start = System.currentTimeMillis();
   while (System.currentTimeMillis() < start + DatabaseDescriptor.getRpcTimeout()) {
     try {
       // zero means 'bind on any available port.'
       socket =
           new Socket(
               endpoint, DatabaseDescriptor.getStoragePort(), InetAddress.getLocalHost(), 0);
       socket.setTcpNoDelay(true);
       output = new DataOutputStream(socket.getOutputStream());
       return true;
     } catch (IOException e) {
       socket = null;
       if (logger.isTraceEnabled()) logger.trace("unable to connect to " + endpoint, e);
       try {
         Thread.sleep(OPEN_RETRY_DELAY);
       } catch (InterruptedException e1) {
         throw new AssertionError(e1);
       }
     }
   }
   return false;
 }