Java Code Examples for org.apache.ratis.util.JavaUtils#memoize()

The following examples show how to use org.apache.ratis.util.JavaUtils#memoize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RaftClientImpl.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
RaftClientImpl(ClientId clientId, RaftGroup group, RaftPeerId leaderId,
    RaftClientRpc clientRpc, RaftProperties properties, RetryPolicy retryPolicy) {
  this.clientId = clientId;
  this.clientRpc = clientRpc;
  this.peers = new ConcurrentLinkedQueue<>(group.getPeers());
  this.groupId = group.getGroupId();
  this.leaderId = leaderId != null? leaderId
      : !peers.isEmpty()? peers.iterator().next().getId(): null;
  Preconditions.assertTrue(retryPolicy != null, "retry policy can't be null");
  this.retryPolicy = retryPolicy;

  scheduler = TimeoutScheduler.getInstance();
  clientRpc.addServers(peers);

  this.orderedAsync = JavaUtils.memoize(() -> OrderedAsync.newInstance(this, properties));
  this.streamApi = JavaUtils.memoize(() -> StreamImpl.newInstance(this, properties));
}
 
Example 2
Source File: RetryPolicies.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private RetryLimited(int maxAttempts, TimeDuration sleepTime) {
  super(sleepTime);

  if (maxAttempts < 0) {
    throw new IllegalArgumentException("maxAttempts = " + maxAttempts+" < 0");
  }

  this.maxAttempts = maxAttempts;
  this.myString = JavaUtils.memoize(() -> getClass().getSimpleName()
      + "(maxAttempts=" + maxAttempts + ", sleepTime=" + sleepTime + ")");
}
 
Example 3
Source File: MultipleLinearRandomRetry.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private MultipleLinearRandomRetry(List<Pair> pairs) {
  if (pairs == null || pairs.isEmpty()) {
    throw new IllegalArgumentException("pairs must be neither null nor empty.");
  }
  this.pairs = Collections.unmodifiableList(pairs);
  this.myString = JavaUtils.memoize(() -> getClass().getSimpleName() + pairs);
}
 
Example 4
Source File: GrpcClientProtocolClient.java    From ratis with Apache License 2.0 5 votes vote down vote up
public GrpcClientProtocolClient(ClientId id, RaftPeer target,
                                RaftProperties properties,
                                GrpcTlsConfig tlsConf) {
  this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
  this.target = target;
  final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
  final SizeInBytes maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug);
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConf!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConf.getTrustStore() != null) {
      sslContextBuilder.trustManager(tlsConf.getTrustStore());
    }
    if (tlsConf.getMtlsEnabled()) {
      sslContextBuilder.keyManager(tlsConf.getCertChain(),
          tlsConf.getPrivateKey());
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(sslContextBuilder.build());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow.getSizeInt())
      .maxInboundMessageSize(maxMessageSize.getSizeInt())
      .build();
  blockingStub = RaftClientProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftClientProtocolServiceGrpc.newStub(channel);
  adminBlockingStub = AdminProtocolServiceGrpc.newBlockingStub(channel);
  this.requestTimeoutDuration = RaftClientConfigKeys.Rpc.requestTimeout(properties);
}
 
Example 5
Source File: FileStore.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public FileStore(Supplier<RaftPeerId> idSupplier, Path dir) {
  this.idSupplier = idSupplier;
  this.rootSupplier = JavaUtils.memoize(
      () -> dir.resolve(getId().toString()).normalize().toAbsolutePath());
  this.files = new FileMap(JavaUtils.memoize(() -> idSupplier.get() + ":files"));
}
 
Example 6
Source File: RaftPeer.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
/** Construct a peer with the given id and address. */
public RaftPeer(RaftPeerId id, String address) {
  this.id = Objects.requireNonNull(id, "id == null");
  this.address = address;
  this.raftPeerProto = JavaUtils.memoize(this::buildRaftPeerProto);
}
 
Example 7
Source File: RaftId.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private RaftId(UUID uuid, Supplier<ByteString> uuidBytes) {
  this.uuid = uuid;
  this.uuidBytes = uuidBytes;
  this.uuidString = JavaUtils.memoize(() -> createUuidString(uuid));
}
 
Example 8
Source File: RaftId.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
RaftId(UUID uuid) {
  this(uuid, JavaUtils.memoize(() -> toByteString(uuid)));
}
 
Example 9
Source File: GrpcClientProtocolClient.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
GrpcClientProtocolClient(ClientId id, RaftPeer target, RaftProperties properties, GrpcTlsConfig tlsConf) {
  this.name = JavaUtils.memoize(() -> id + "->" + target.getId());
  this.target = target;
  final SizeInBytes flowControlWindow = GrpcConfigKeys.flowControlWindow(properties, LOG::debug);
  final SizeInBytes maxMessageSize = GrpcConfigKeys.messageSizeMax(properties, LOG::debug);
  NettyChannelBuilder channelBuilder =
      NettyChannelBuilder.forTarget(target.getAddress());

  if (tlsConf!= null) {
    SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
    if (tlsConf.isFileBasedConfig()) {
      sslContextBuilder.trustManager(tlsConf.getTrustStoreFile());
    } else {
      sslContextBuilder.trustManager(tlsConf.getTrustStore());
    }
    if (tlsConf.getMtlsEnabled()) {
      if (tlsConf.isFileBasedConfig()) {
        sslContextBuilder.keyManager(tlsConf.getCertChainFile(),
            tlsConf.getPrivateKeyFile());
      } else {
        sslContextBuilder.keyManager(tlsConf.getPrivateKey(),
            tlsConf.getCertChain());
      }
    }
    try {
      channelBuilder.useTransportSecurity().sslContext(
          sslContextBuilder.build());
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  } else {
    channelBuilder.negotiationType(NegotiationType.PLAINTEXT);
  }
  channel = channelBuilder.flowControlWindow(flowControlWindow.getSizeInt())
      .maxInboundMessageSize(maxMessageSize.getSizeInt())
      .build();
  blockingStub = RaftClientProtocolServiceGrpc.newBlockingStub(channel);
  asyncStub = RaftClientProtocolServiceGrpc.newStub(channel);
  adminBlockingStub = AdminProtocolServiceGrpc.newBlockingStub(channel);
  this.requestTimeoutDuration = RaftClientConfigKeys.Rpc.requestTimeout(properties);
  this.watchRequestTimeoutDuration =
      RaftClientConfigKeys.Rpc.watchRequestTimeout(properties);
}
 
Example 10
Source File: RaftOutputStream.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftOutputStream(Supplier<RaftClient> clientSupplier, SizeInBytes bufferSize) {
  this.client = JavaUtils.memoize(clientSupplier);
  this.buffer = new byte[bufferSize.getSizeInt()];
}
 
Example 11
Source File: RaftServerRpcWithProxy.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public RaftServerRpcWithProxy(Supplier<RaftPeerId> idSupplier, Function<RaftPeerId, PROXIES> proxyCreater) {
  this.idSupplier = idSupplier;
  this.lifeCycleSupplier = JavaUtils.memoize(() -> new LifeCycle(getId() + "-" + getClass().getSimpleName()));
  this.proxiesSupplier = JavaUtils.memoize(() -> proxyCreater.apply(getId()));
}
 
Example 12
Source File: FileStore.java    From ratis with Apache License 2.0 4 votes vote down vote up
public FileStore(Supplier<RaftPeerId> idSupplier, Path dir) {
  this.idSupplier = idSupplier;
  this.rootSupplier = JavaUtils.memoize(
      () -> dir.resolve(getId().toString()).normalize().toAbsolutePath());
  this.files = new FileMap(JavaUtils.memoize(() -> idSupplier.get() + ":files"));
}
 
Example 13
Source File: RaftPeer.java    From ratis with Apache License 2.0 4 votes vote down vote up
/** Construct a peer with the given id and address. */
public RaftPeer(RaftPeerId id, String address) {
  this.id = Objects.requireNonNull(id, "id == null");
  this.address = address;
  this.raftPeerProto = JavaUtils.memoize(this::buildRaftPeerProto);
}
 
Example 14
Source File: RaftId.java    From ratis with Apache License 2.0 4 votes vote down vote up
private RaftId(UUID uuid, Supplier<ByteString> uuidBytes) {
  this.uuid = uuid;
  this.uuidBytes = uuidBytes;
  this.uuidString = JavaUtils.memoize(() -> createUuidString(uuid));
}
 
Example 15
Source File: RaftId.java    From ratis with Apache License 2.0 4 votes vote down vote up
RaftId(UUID uuid) {
  this(uuid, JavaUtils.memoize(() -> toByteString(uuid)));
}
 
Example 16
Source File: RaftServerRpcWithProxy.java    From ratis with Apache License 2.0 4 votes vote down vote up
public RaftServerRpcWithProxy(Supplier<RaftPeerId> idSupplier, Function<RaftPeerId, PROXIES> proxyCreater) {
  this.idSupplier = idSupplier;
  this.lifeCycleSupplier = JavaUtils.memoize(() -> new LifeCycle(getId() + "-" + getClass().getSimpleName()));
  this.proxiesSupplier = JavaUtils.memoize(() -> proxyCreater.apply(getId()));
}