Пример #1
0
  @Override
  public void write(MetaData metaData) throws GatewayException {
    final String newMetaData = "metadata-" + (currentIndex + 1);
    CachedStreamOutput.Entry cachedEntry = CachedStreamOutput.popEntry();
    try {
      StreamOutput streamOutput;
      if (compress) {
        streamOutput = cachedEntry.bytes(CompressorFactory.defaultCompressor());
      } else {
        streamOutput = cachedEntry.bytes();
      }
      XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, streamOutput);
      builder.startObject();
      MetaData.Builder.toXContent(metaData, builder, ToXContent.EMPTY_PARAMS);
      builder.endObject();
      builder.close();
      metaDataBlobContainer.writeBlob(
          newMetaData,
          new ByteArrayInputStream(
              cachedEntry.bytes().underlyingBytes(), 0, cachedEntry.bytes().size()),
          cachedEntry.bytes().size());
    } catch (IOException e) {
      throw new GatewayException("Failed to write metadata [" + newMetaData + "]", e);
    } finally {
      CachedStreamOutput.pushEntry(cachedEntry);
    }

    currentIndex++;

    try {
      metaDataBlobContainer.deleteBlobsByFilter(
          new BlobContainer.BlobNameFilter() {
            @Override
            public boolean accept(String blobName) {
              return blobName.startsWith("metadata-") && !newMetaData.equals(blobName);
            }
          });
    } catch (IOException e) {
      logger.debug("Failed to delete old metadata, will do it next time", e);
    }
  }
 @Override
 public void sendResponse(Throwable error) throws IOException {
   BytesStreamOutput stream;
   try {
     stream = CachedStreamOutput.cachedBytes();
     writeResponseExceptionHeader(stream);
     RemoteTransportException tx =
         new RemoteTransportException(
             targetTransport.nodeName(),
             targetTransport.boundAddress().boundAddress(),
             action,
             error);
     ThrowableObjectOutputStream too = new ThrowableObjectOutputStream(stream);
     too.writeObject(tx);
     too.close();
   } catch (NotSerializableException e) {
     stream = CachedStreamOutput.cachedBytes();
     writeResponseExceptionHeader(stream);
     RemoteTransportException tx =
         new RemoteTransportException(
             targetTransport.nodeName(),
             targetTransport.boundAddress().boundAddress(),
             action,
             new NotSerializableTransportException(error));
     ThrowableObjectOutputStream too = new ThrowableObjectOutputStream(stream);
     too.writeObject(tx);
     too.close();
   }
   final byte[] data = stream.copiedByteArray();
   targetTransport
       .threadPool()
       .execute(
           new Runnable() {
             @Override
             public void run() {
               targetTransport.messageReceived(data, action, sourceTransport, null);
             }
           });
 }
 @Override
 public void sendResponse(Streamable message, TransportResponseOptions options)
     throws IOException {
   HandlesStreamOutput stream = CachedStreamOutput.cachedHandlesBytes();
   stream.writeLong(requestId);
   byte status = 0;
   status = TransportStreams.statusSetResponse(status);
   stream.writeByte(status); // 0 for request, 1 for response.
   message.writeTo(stream);
   final byte[] data = ((BytesStreamOutput) stream.wrappedOut()).copiedByteArray();
   targetTransport
       .threadPool()
       .execute(
           new Runnable() {
             @Override
             public void run() {
               targetTransport.messageReceived(data, action, sourceTransport, null);
             }
           });
 }
Пример #4
0
 public static byte[] toBytes(ClusterState state) throws IOException {
   BytesStreamOutput os = CachedStreamOutput.cachedBytes();
   writeTo(state, os);
   return os.copiedByteArray();
 }