use of io.pravega.client.stream.impl.ControllerImpl in project pravega by pravega.
the class PravegaAuthManagerTest method registerInterceptors.
@Test
public void registerInterceptors() throws Exception {
// Test the registration method.
GRPCServerConfig config = GRPCServerConfigImpl.builder().authorizationEnabled(true).userPasswordFile(file.getAbsolutePath()).port(1000).build();
PravegaAuthManager manager = new PravegaAuthManager(config);
int port = TestUtils.getAvailableListenPort();
ServerBuilder<?> server = ServerBuilder.forPort(port).useTransportSecurity(new File("../config/cert.pem"), new File("../config/key.pem"));
server.addService(serviceImpl);
manager.registerInterceptors(server);
server.build().start();
InlineExecutor executor = new InlineExecutor();
Credentials creds = new DefaultCredentials("1111_aaaa", "admin");
final ControllerImpl controllerClient = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(URI.create("tcp://localhost:" + port)).build()).retryAttempts(1).build(), executor);
MultivaluedMap<String, String> map = new MultivaluedHashMap();
// Without specifying a valid handler.
assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
// Non existent interceptor method.
map.add("method", "invalid");
assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
// Specify a valid method but no parameters for default interceptor.
map.putSingle("method", "Pravega-Default");
assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
// Specify a valid method but no password for default interceptor.
map.putSingle("username", "dummy3");
assertThrows(AuthenticationException.class, () -> manager.authenticate("hi", map, AuthHandler.Permissions.READ));
// Specify a valid method and parameters but invalid resource for default interceptor.
map.putSingle("password", "password");
assertFalse("Not existent resource should return false", manager.authenticate("invalid", map, AuthHandler.Permissions.READ));
// Valid parameters for default interceptor
map.putSingle("username", "dummy3");
map.putSingle("password", "password");
assertTrue("Read access for read resource should return true", manager.authenticate("readresource", map, AuthHandler.Permissions.READ));
// Stream/scope access should be extended to segment.
assertTrue("Read access for read resource should return true", manager.authenticate("readresource/segment", map, AuthHandler.Permissions.READ));
// Levels of access
assertFalse("Write access for read resource should return false", manager.authenticate("readresource", map, AuthHandler.Permissions.READ_UPDATE));
assertTrue("Read access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ));
assertTrue("Write access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ_UPDATE));
// Check the wildcard access
map.putSingle("username", "dummy4");
assertTrue("Write access for write resource should return true", manager.authenticate("totalaccess", map, AuthHandler.Permissions.READ_UPDATE));
map.putSingle("method", "testHandler");
assertTrue("Test handler should be called", manager.authenticate("any", map, AuthHandler.Permissions.READ));
assertThrows(RetriesExhaustedException.class, () -> controllerClient.createScope("hi").join());
}
use of io.pravega.client.stream.impl.ControllerImpl in project pravega by pravega.
the class PravegaTest method createStream.
/**
* Invoke the createStream method, ensure we are able to create stream.
*
* @throws InterruptedException if interrupted
* @throws URISyntaxException If URI is invalid
* @throws ExecutionException if error in create stream
*/
@Before
public void createStream() throws InterruptedException, ExecutionException {
Service conService = Utils.createPravegaControllerService(null);
List<URI> ctlURIs = conService.getServiceDetails();
URI controllerUri = ctlURIs.get(0);
log.info("Invoking create stream with Controller URI: {}", controllerUri);
@Cleanup ConnectionFactory connectionFactory = new ConnectionFactoryImpl(ClientConfig.builder().build());
ControllerImpl controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(controllerUri).build()).build(), connectionFactory.getInternalExecutor());
assertTrue(controller.createScope(STREAM_SCOPE).get());
assertTrue(controller.createStream(config).get());
}
use of io.pravega.client.stream.impl.ControllerImpl in project pravega by pravega.
the class SetupUtils method startAllServices.
/**
* Start all pravega related services required for the test deployment.
*
* @param numThreads the number of threads for the internal client threadpool.
* @throws Exception on any errors.
*/
public void startAllServices(Integer numThreads) throws Exception {
if (!this.started.compareAndSet(false, true)) {
log.warn("Services already started, not attempting to start again");
return;
}
this.connectionFactory = new ConnectionFactoryImpl(clientConfig, numThreads);
this.controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(clientConfig).build(), connectionFactory.getInternalExecutor());
this.clientFactory = new ClientFactoryImpl(scope, controller, connectionFactory);
// Start zookeeper.
this.zkTestServer = new TestingServerStarter().start();
this.zkTestServer.start();
// Start Pravega Service.
ServiceBuilder serviceBuilder = ServiceBuilder.newInMemoryBuilder(ServiceBuilderConfig.getDefaultConfig());
serviceBuilder.initialize();
StreamSegmentStore store = serviceBuilder.createStreamSegmentService();
this.server = new PravegaConnectionListener(false, servicePort, store);
this.server.startListening();
log.info("Started Pravega Service");
// Start Controller.
this.controllerWrapper = new ControllerWrapper(this.zkTestServer.getConnectString(), false, true, controllerRPCPort, "localhost", servicePort, Config.HOST_STORE_CONTAINER_COUNT, controllerRESTPort);
this.controllerWrapper.awaitRunning();
this.controllerWrapper.getController().createScope(scope).get();
log.info("Initialized Pravega Controller");
}
use of io.pravega.client.stream.impl.ControllerImpl in project pravega by pravega.
the class AutoScaleTest method scaleDownTest.
/**
* Invoke the simple scale down Test, produce no into a stream.
* The test will periodically check if a scale event has occured by talking to controller via
* controller client.
*
* @throws InterruptedException if interrupted
* @throws URISyntaxException If URI is invalid
*/
private CompletableFuture<Void> scaleDownTest() {
final ControllerImpl controller = getController();
final AtomicBoolean exit = new AtomicBoolean(false);
// overall wait for test to complete in 260 seconds (4.2 minutes) or scale down, whichever happens first.
return Retry.withExpBackoff(10, 10, 30, Duration.ofSeconds(10).toMillis()).retryingOn(ScaleOperationNotDoneException.class).throwingOn(RuntimeException.class).runAsync(() -> controller.getCurrentSegments(SCOPE, SCALE_DOWN_STREAM_NAME).thenAccept(x -> {
if (x.getSegments().size() == 2) {
throw new ScaleOperationNotDoneException();
} else {
log.info("scale down done successfully");
exit.set(true);
}
}), EXECUTOR_SERVICE);
}
use of io.pravega.client.stream.impl.ControllerImpl in project pravega by pravega.
the class ControllerRestApiTest method restApiTests.
@Test(timeout = 300000)
public void restApiTests() {
Service conService = Utils.createPravegaControllerService(null);
List<URI> ctlURIs = conService.getServiceDetails();
URI controllerRESTUri = ctlURIs.get(1);
Invocation.Builder builder;
Response response;
restServerURI = "http://" + controllerRESTUri.getHost() + ":" + controllerRESTUri.getPort();
log.info("REST Server URI: {}", restServerURI);
// TEST REST server status, ping test
resourceURl = new StringBuilder(restServerURI).append("/ping").toString();
webTarget = client.target(resourceURl);
builder = webTarget.request();
response = builder.get();
assertEquals("Ping test", OK.getStatusCode(), response.getStatus());
log.info("REST Server is running. Ping successful.");
final String scope1 = RandomStringUtils.randomAlphanumeric(10);
final String stream1 = RandomStringUtils.randomAlphanumeric(10);
// TEST CreateScope POST http://controllerURI:Port/v1/scopes
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes").toString();
webTarget = client.target(resourceURl);
final CreateScopeRequest createScopeRequest = new CreateScopeRequest();
createScopeRequest.setScopeName(scope1);
builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
response = builder.post(Entity.json(createScopeRequest));
assertEquals("Create scope status", CREATED.getStatusCode(), response.getStatus());
Assert.assertEquals("Create scope response", scope1, response.readEntity(ScopeProperty.class).getScopeName());
log.info("Create scope: {} successful ", scope1);
// TEST CreateStream POST http://controllerURI:Port/v1/scopes/{scopeName}/streams
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams").toString();
webTarget = client.target(resourceURl);
CreateStreamRequest createStreamRequest = new CreateStreamRequest();
ScalingConfig scalingConfig = new ScalingConfig();
scalingConfig.setType(ScalingConfig.TypeEnum.FIXED_NUM_SEGMENTS);
scalingConfig.setTargetRate(2);
scalingConfig.scaleFactor(2);
scalingConfig.minSegments(2);
RetentionConfig retentionConfig = new RetentionConfig();
retentionConfig.setType(RetentionConfig.TypeEnum.LIMITED_DAYS);
retentionConfig.setValue(123L);
createStreamRequest.setStreamName(stream1);
createStreamRequest.setScalingPolicy(scalingConfig);
createStreamRequest.setRetentionPolicy(retentionConfig);
builder = webTarget.request(MediaType.APPLICATION_JSON_TYPE);
response = builder.post(Entity.json(createStreamRequest));
assertEquals("Create stream status", CREATED.getStatusCode(), response.getStatus());
final StreamProperty streamPropertyResponse = response.readEntity(StreamProperty.class);
assertEquals("Scope name in response", scope1, streamPropertyResponse.getScopeName());
assertEquals("Stream name in response", stream1, streamPropertyResponse.getStreamName());
log.info("Create stream: {} successful", stream1);
// Test listScopes GET http://controllerURI:Port/v1/scopes/{scopeName}/streams
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes").toString();
webTarget = client.target(resourceURl);
builder = webTarget.request();
response = builder.get();
assertEquals("List scopes", OK.getStatusCode(), response.getStatus());
log.info("List scopes successful");
// Test listStream GET /v1/scopes/scope1/streams
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams").toString();
webTarget = client.target(resourceURl);
builder = webTarget.request();
response = builder.get();
assertEquals("List streams", OK.getStatusCode(), response.getStatus());
Assert.assertEquals("List streams size", 1, response.readEntity(StreamsList.class).getStreams().size());
log.info("List streams successful");
// Test getScope
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1).toString();
response = client.target(resourceURl).request().get();
assertEquals("Get scope status", OK.getStatusCode(), response.getStatus());
assertEquals("Get scope scope1 response", scope1, response.readEntity(ScopeProperty.class).getScopeName());
log.info("Get scope successful");
// Test updateStream
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1).toString();
UpdateStreamRequest updateStreamRequest = new UpdateStreamRequest();
ScalingConfig scalingConfig1 = new ScalingConfig();
scalingConfig1.setType(ScalingConfig.TypeEnum.FIXED_NUM_SEGMENTS);
scalingConfig1.setTargetRate(2);
// update existing scaleFactor from 2 to 3
scalingConfig1.scaleFactor(3);
// update existing minSegments from 2 to 4
scalingConfig1.minSegments(4);
updateStreamRequest.setScalingPolicy(scalingConfig1);
updateStreamRequest.setRetentionPolicy(retentionConfig);
response = client.target(resourceURl).request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(updateStreamRequest));
assertEquals("Update stream status", OK.getStatusCode(), response.getStatus());
assertEquals("Verify updated property", 4, response.readEntity(StreamProperty.class).getScalingPolicy().getMinSegments().intValue());
log.info("Update stream successful");
// Test getStream
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1).toString();
response = client.target(resourceURl).request().get();
assertEquals("Get stream status", OK.getStatusCode(), response.getStatus());
assertEquals("Get stream stream1 response", stream1, response.readEntity(StreamProperty.class).getStreamName());
log.info("Get stream successful");
// Test updateStreamState
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1 + "/state").toString();
StreamState streamState = new StreamState();
streamState.setStreamState(StreamState.StreamStateEnum.SEALED);
response = client.target(resourceURl).request(MediaType.APPLICATION_JSON_TYPE).put(Entity.json(streamState));
assertEquals("UpdateStreamState status", OK.getStatusCode(), response.getStatus());
assertEquals("UpdateStreamState status in response", streamState.getStreamState(), response.readEntity(StreamState.class).getStreamState());
log.info("Update stream state successful");
// Test deleteStream
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1 + "/streams/" + stream1).toString();
response = client.target(resourceURl).request().delete();
assertEquals("DeleteStream status", NO_CONTENT.getStatusCode(), response.getStatus());
log.info("Delete stream successful");
// Test deleteScope
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + scope1).toString();
response = client.target(resourceURl).request().delete();
assertEquals("Get scope status", NO_CONTENT.getStatusCode(), response.getStatus());
log.info("Delete Scope successful");
// Test reader groups APIs.
// Prepare the streams and readers using the admin client.
final String testScope = RandomStringUtils.randomAlphanumeric(10);
final String testStream1 = RandomStringUtils.randomAlphanumeric(10);
final String testStream2 = RandomStringUtils.randomAlphanumeric(10);
URI controllerUri = ctlURIs.get(0);
try (StreamManager streamManager = new StreamManagerImpl(ClientConfig.builder().controllerURI(controllerUri).build())) {
log.info("Creating scope: {}", testScope);
streamManager.createScope(testScope);
log.info("Creating stream: {}", testStream1);
StreamConfiguration streamConf1 = StreamConfiguration.builder().scope(testScope).streamName(testStream1).scalingPolicy(ScalingPolicy.fixed(1)).build();
streamManager.createStream(testScope, testStream1, streamConf1);
log.info("Creating stream: {}", testStream2);
StreamConfiguration streamConf2 = StreamConfiguration.builder().scope(testScope).streamName(testStream2).scalingPolicy(ScalingPolicy.fixed(1)).build();
streamManager.createStream(testScope, testStream2, streamConf2);
}
final String readerGroupName1 = RandomStringUtils.randomAlphanumeric(10);
final String readerGroupName2 = RandomStringUtils.randomAlphanumeric(10);
final String reader1 = RandomStringUtils.randomAlphanumeric(10);
final String reader2 = RandomStringUtils.randomAlphanumeric(10);
@Cleanup("shutdown") InlineExecutor executor = new InlineExecutor();
Controller controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(ClientConfig.builder().controllerURI(controllerUri).build()).build(), executor);
try (ClientFactory clientFactory = new ClientFactoryImpl(testScope, controller);
ReaderGroupManager readerGroupManager = ReaderGroupManager.withScope(testScope, ClientConfig.builder().controllerURI(controllerUri).build())) {
final ReaderGroupConfig config = ReaderGroupConfig.builder().stream(Stream.of(testScope, testStream1)).stream(Stream.of(testScope, testStream2)).build();
readerGroupManager.createReaderGroup(readerGroupName1, config);
readerGroupManager.createReaderGroup(readerGroupName2, config);
clientFactory.createReader(reader1, readerGroupName1, new JavaSerializer<Long>(), ReaderConfig.builder().build());
clientFactory.createReader(reader2, readerGroupName1, new JavaSerializer<Long>(), ReaderConfig.builder().build());
}
// Verify the reader group info using REST APIs.
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + testScope + "/readergroups").toString();
response = client.target(resourceURl).request().get();
assertEquals("Get readergroups status", OK.getStatusCode(), response.getStatus());
ReaderGroupsList readerGroupsList = response.readEntity(ReaderGroupsList.class);
assertEquals("Get readergroups size", 2, readerGroupsList.getReaderGroups().size());
assertTrue(readerGroupsList.getReaderGroups().contains(new ReaderGroupsListReaderGroups().readerGroupName(readerGroupName1)));
assertTrue(readerGroupsList.getReaderGroups().contains(new ReaderGroupsListReaderGroups().readerGroupName(readerGroupName2)));
log.info("Get readergroups successful");
// Test fetching readergroup info.
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + testScope + "/readergroups/" + readerGroupName1).toString();
response = client.target(resourceURl).request().get();
assertEquals("Get readergroup properties status", OK.getStatusCode(), response.getStatus());
ReaderGroupProperty readerGroupProperty = response.readEntity(ReaderGroupProperty.class);
assertEquals("Get readergroup name", readerGroupName1, readerGroupProperty.getReaderGroupName());
assertEquals("Get readergroup scope name", testScope, readerGroupProperty.getScopeName());
assertEquals("Get readergroup streams size", 2, readerGroupProperty.getStreamList().size());
assertTrue(readerGroupProperty.getStreamList().contains(testStream1));
assertTrue(readerGroupProperty.getStreamList().contains(testStream2));
assertEquals("Get readergroup onlinereaders size", 2, readerGroupProperty.getOnlineReaderIds().size());
assertTrue(readerGroupProperty.getOnlineReaderIds().contains(reader1));
assertTrue(readerGroupProperty.getOnlineReaderIds().contains(reader2));
// Test readergroup or scope not found.
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + testScope + "/readergroups/" + "unknownreadergroup").toString();
response = client.target(resourceURl).request().get();
assertEquals("Get readergroup properties status", NOT_FOUND.getStatusCode(), response.getStatus());
resourceURl = new StringBuilder(restServerURI).append("/v1/scopes/" + "unknownscope" + "/readergroups/" + readerGroupName1).toString();
response = client.target(resourceURl).request().get();
assertEquals("Get readergroup properties status", NOT_FOUND.getStatusCode(), response.getStatus());
log.info("Get readergroup properties successful");
log.info("Test restApiTests passed successfully!");
}
Aggregations