use of io.pravega.client.security.auth.DelegationTokenProvider in project pravega by pravega.
the class LargeEventWriter method writeLargeEvent.
/**
* Write the provided list of events (atomically) to the provided segment.
*
* @param segment The segment to write to
* @param events The events to append
* @param tokenProvider A token provider
* @param config Used for retry configuration parameters
* @throws NoSuchSegmentException If the provided segment does not exit.
* @throws SegmentSealedException If the segment is sealed.
* @throws AuthenticationException If the token can't be used for this segment.
* @throws UnsupportedOperationException If the server does not support large events.
*/
public void writeLargeEvent(Segment segment, List<ByteBuffer> events, DelegationTokenProvider tokenProvider, EventWriterConfig config) throws NoSuchSegmentException, AuthenticationException, SegmentSealedException {
List<ByteBuf> payloads = createBufs(events);
int attempts = 1 + Math.max(0, config.getRetryAttempts());
Retry.withExpBackoff(config.getInitialBackoffMillis(), config.getBackoffMultiple(), attempts, config.getMaxBackoffMillis()).retryWhen(t -> {
Throwable ex = Exceptions.unwrap(t);
if (ex instanceof ConnectionFailedException) {
log.info("Connection failure while sending large event: {}. Retrying", ex.getMessage());
return true;
} else if (ex instanceof TokenExpiredException) {
tokenProvider.signalTokenExpired();
log.info("Authentication token expired while writing large event to segment {}. Retrying", segment);
return true;
} else {
return false;
}
}).run(() -> {
@Cleanup RawClient client = new RawClient(controller, connectionPool, segment);
write(segment, payloads, client, tokenProvider);
return null;
});
}
use of io.pravega.client.security.auth.DelegationTokenProvider in project pravega by pravega.
the class ClientFactoryImpl method createRevisionedStreamClient.
private <T> RevisionedStreamClient<T> createRevisionedStreamClient(Segment segment, Serializer<T> serializer, SynchronizerConfig config) {
EventSegmentReader in = inFactory.createEventReaderForSegment(segment, config.getReadBufferSize());
DelegationTokenProvider delegationTokenProvider = DelegationTokenProviderFactory.create(controller, segment, AccessOperation.READ_WRITE);
ConditionalOutputStream cond = condFactory.createConditionalOutputStream(segment, delegationTokenProvider, config.getEventWriterConfig());
SegmentMetadataClient meta = metaFactory.createSegmentMetadataClient(segment, delegationTokenProvider);
return new RevisionedStreamClientImpl<>(segment, in, outFactory, cond, meta, serializer, config.getEventWriterConfig(), delegationTokenProvider);
}
use of io.pravega.client.security.auth.DelegationTokenProvider in project pravega by pravega.
the class ByteStreanWriterImplTest method setup.
@Before
public void setup() {
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", 0);
connectionFactory = new MockConnectionFactoryImpl();
ClientConnection connection = mock(ClientConnection.class);
connectionFactory.provideConnection(endpoint, connection);
controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, false);
controller.createScope(SCOPE);
controller.createStream(SCOPE, STREAM, StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
MockSegmentStreamFactory streamFactory = new MockSegmentStreamFactory();
StreamSegments segments = Futures.getThrowingException(controller.getCurrentSegments(SCOPE, STREAM));
Preconditions.checkState(segments.getNumberOfSegments() > 0, "Stream is sealed");
Preconditions.checkState(segments.getNumberOfSegments() == 1, "Stream is configured with more than one segment");
Segment segment = segments.getSegments().iterator().next();
EventWriterConfig config = EventWriterConfig.builder().retryAttempts(Integer.MAX_VALUE).build();
DelegationTokenProvider tokenProvider = DelegationTokenProviderFactory.create(controller, segment, AccessOperation.WRITE);
mockWriter = new ByteStreamWriterImpl(streamFactory.createOutputStreamForSegment(segment, config, tokenProvider), streamFactory.createSegmentMetadataClient(segment, tokenProvider));
}
use of io.pravega.client.security.auth.DelegationTokenProvider in project pravega by pravega.
the class AsyncSegmentInputStreamTest method testRetry.
@Test(timeout = 10000)
public void testRetry() throws ConnectionFailedException {
Segment segment = new Segment("scope", "testRetry", 4);
PravegaNodeUri endpoint = new PravegaNodeUri("localhost", SERVICE_PORT);
@Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
@Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
DelegationTokenProvider tokenProvider = mock(DelegationTokenProvider.class);
// return empty token
when(tokenProvider.retrieveToken()).thenReturn(CompletableFuture.completedFuture(""));
Semaphore dataAvailable = new Semaphore(0);
@Cleanup AsyncSegmentInputStreamImpl in = new AsyncSegmentInputStreamImpl(controller, connectionFactory, segment, tokenProvider, dataAvailable);
ClientConnection c = mock(ClientConnection.class);
InOrder inOrder = Mockito.inOrder(c);
connectionFactory.provideConnection(endpoint, c);
WireCommands.SegmentRead segmentRead = new WireCommands.SegmentRead(segment.getScopedName(), 1234, false, false, Unpooled.EMPTY_BUFFER, in.getRequestId());
Mockito.doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).connectionDropped();
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).authTokenCheckFailed(new WireCommands.AuthTokenCheckFailed(in.getRequestId(), "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED));
return null;
}
}).doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
connectionFactory.getProcessor(endpoint).process(segmentRead);
return null;
}
}).when(c).send(any(ReadSegment.class));
assertEquals(0, dataAvailable.availablePermits());
CompletableFuture<SegmentRead> readFuture = in.read(1234, 5678);
assertEquals(segmentRead, readFuture.join());
assertTrue(Futures.isSuccessful(readFuture));
assertEquals(1, dataAvailable.availablePermits());
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
inOrder.verify(c).close();
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
inOrder.verify(c).close();
inOrder.verify(c).send(eq(new WireCommands.ReadSegment(segment.getScopedName(), 1234, 5678, "", in.getRequestId())));
verifyNoMoreInteractions(c);
// ensure retrieve Token is invoked for every retry.
verify(tokenProvider, times(3)).retrieveToken();
}
use of io.pravega.client.security.auth.DelegationTokenProvider in project pravega by pravega.
the class ByteStreamClientImpl method createByteStreamWriter.
@Override
public ByteStreamWriter createByteStreamWriter(String streamName) {
StreamSegments segments = Futures.getThrowingException(controller.getCurrentSegments(scope, streamName));
Preconditions.checkState(segments.getNumberOfSegments() > 0, "Stream is sealed");
Preconditions.checkState(segments.getNumberOfSegments() == 1, "Stream is configured with more than one segment");
Segment segment = segments.getSegments().iterator().next();
// The writer should not give up connecting to SegmentStore in the background until the ByteStreamWriter is closed.
EventWriterConfig config = EventWriterConfig.builder().retryAttempts(Integer.MAX_VALUE).build();
DelegationTokenProvider tokenProvider = DelegationTokenProviderFactory.create(controller, segment, AccessOperation.WRITE);
return new BufferedByteStreamWriterImpl(new ByteStreamWriterImpl(outputStreamFactory.createOutputStreamForSegment(segment, config, tokenProvider), metaStreamFactory.createSegmentMetadataClient(segment, tokenProvider)));
}
Aggregations