use of com.linkedin.r2.util.NamedThreadFactory in project rest.li by linkedin.
the class TestRequestCompression method testUpdate.
@Test(dataProvider = "requestData", retryAnalyzer = ThreeRetries.class)
public void testUpdate(CompressionConfig requestCompressionConfig, String supportedEncodings, RestliRequestOptions restliRequestOptions, int messageLength, String testHelpHeader) throws RemoteInvocationException, CloneNotSupportedException, InterruptedException, ExecutionException, TimeoutException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
Map<String, CompressionConfig> requestCompressionConfigs = new HashMap<>();
if (requestCompressionConfig != null) {
requestCompressionConfigs.put(SERVICE_NAME, requestCompressionConfig);
}
HttpClientFactory httpClientFactory = new HttpClientFactory.Builder().setFilterChain(FilterChains.empty()).setEventLoopGroup(new NioEventLoopGroup()).setShutDownFactory(true).setScheduleExecutorService(executor).setShutdownScheduledExecutorService(true).setCallbackExecutor(null).setShutdownCallbackExecutor(false).setJmxManager(AbstractJmxManager.NULL_JMX_MANAGER).setRequestCompressionThresholdDefault(500).setRequestCompressionConfigs(requestCompressionConfigs).build();
Map<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_REQUEST_CONTENT_ENCODINGS, supportedEncodings);
properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
RootBuilderWrapper<Long, Greeting> builders = new RootBuilderWrapper<>(new GreetingsRequestBuilders(restliRequestOptions));
// GET
Request<Greeting> request = builders.get().id(1L).build();
ResponseFuture<Greeting> future = client.sendRequest(request);
Response<Greeting> greetingResponse = future.getResponse();
String response1 = greetingResponse.getEntity().getMessage();
Assert.assertNotNull(response1);
// POST
Greeting greeting = new Greeting(greetingResponse.getEntity().data().copy());
char[] As = new char[messageLength];
Arrays.fill(As, 'A');
String message = new String(As);
greeting.setMessage(message);
Request<EmptyRecord> writeRequest = builders.update().id(1L).input(greeting).setHeader(TEST_HELP_HEADER, testHelpHeader).build();
client.sendRequest(writeRequest).getResponse();
// GET again, to verify that our POST worked.
Request<Greeting> request2 = builders.get().id(1L).build();
ResponseFuture<Greeting> future2 = client.sendRequest(request2);
String response2 = future2.getResponse().getEntity().getMessage();
Assert.assertEquals(response2, message);
FutureCallback<None> callback1 = new FutureCallback<>();
client.shutdown(callback1);
callback1.get(30, TimeUnit.SECONDS);
FutureCallback<None> callback2 = new FutureCallback<>();
httpClientFactory.shutdown(callback2);
callback2.get(30, TimeUnit.SECONDS);
}
use of com.linkedin.r2.util.NamedThreadFactory in project rest.li by linkedin.
the class HttpNettyServer method start.
@Override
public void start() {
_eventExecutors = new DefaultEventExecutorGroup(_threadPoolSize);
_bossGroup = new NioEventLoopGroup(1, new NamedThreadFactory("R2 Nio Boss"));
_workerGroup = new NioEventLoopGroup(0, new NamedThreadFactory("R2 Nio Worker"));
final HttpNettyServerPipelineInitializer pipelineInitializer = new HttpNettyServerPipelineInitializer(_dispatcher, _eventExecutors, _sslContext, _sslParameters, _restOverStream);
ServerBootstrap bootstrap = new ServerBootstrap().group(_bossGroup, _workerGroup).channel(NioServerSocketChannel.class).childHandler(pipelineInitializer);
bootstrap.bind(new InetSocketAddress(_port)).awaitUninterruptibly(_startupTimeoutMillis);
}
use of com.linkedin.r2.util.NamedThreadFactory in project rest.li by linkedin.
the class StateUpdaterTest method testExecutorScheduleWithError.
@Test
public void testExecutorScheduleWithError() throws InterruptedException {
D2RelativeStrategyProperties relativeStrategyProperties = new D2RelativeStrategyProperties().setInitialHealthScore(0.01).setUpdateIntervalMs(10);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("D2 TestExecutor"));
setup(relativeStrategyProperties, new ConcurrentHashMap<>(), executor);
List<TrackerClient> trackerClients = TrackerClientMockHelper.mockTrackerClients(2, Arrays.asList(20, 20), Arrays.asList(10, 10), Arrays.asList(200L, 200L), Arrays.asList(100L, 100L), Arrays.asList(0, 0));
_stateUpdater.updateState(new HashSet<>(trackerClients), 0, DEFAULT_CLUSTER_GENERATION_ID, false);
Mockito.doThrow(new NullPointerException()).when(_quarantineManager).updateQuarantineState(Mockito.any(PartitionState.class), Mockito.any(PartitionState.class), anyLong());
assertEquals(_stateUpdater.getPointsMap(0).get(trackerClients.get(0).getUri()).intValue(), 1);
Thread.sleep(21);
assertEquals(_stateUpdater.getPointsMap(0).get(trackerClients.get(0).getUri()).intValue(), 1, "The points did not change due to the failure in each interval execution");
// Verify that the quarantine manager is invoked in each interval, so the tasks are not cancelled
Mockito.verify(_quarantineManager, Mockito.atLeast(3)).updateQuarantineState(Mockito.any(PartitionState.class), Mockito.any(PartitionState.class), anyLong());
executor.shutdown();
}
use of com.linkedin.r2.util.NamedThreadFactory in project rest.li by linkedin.
the class LoadBalancerEchoClient method getLoadBalancer.
public static SimpleLoadBalancer getLoadBalancer(String hostPort) throws IOException, PropertyStoreException {
// zk stores
ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry = null;
ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry = null;
ZooKeeperEphemeralStore<UriProperties> zkUriRegistry = null;
ZKConnection zkClient = new ZKConnection(hostPort, 10000);
zkClusterRegistry = new ZooKeeperPermanentStore<>(zkClient, new ClusterPropertiesJsonSerializer(), _basePath + "/clusters");
zkServiceRegistry = new ZooKeeperPermanentStore<>(zkClient, new ServicePropertiesJsonSerializer(), _basePath + "/services");
zkUriRegistry = new ZooKeeperEphemeralStore<>(zkClient, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), _basePath + "/uris", false, true);
// fs stores
File testDirectory = LoadBalancerUtil.createTempDirectory("lb-degrader-witih-file-store-large");
testDirectory.deleteOnExit();
new File(testDirectory + File.separator + "cluster").mkdir();
new File(testDirectory + File.separator + "service").mkdir();
new File(testDirectory + File.separator + "uri").mkdir();
FileStore<ClusterProperties> fsClusterStore = new FileStore<>(testDirectory + File.separator + "cluster", FileSystemDirectory.FILE_STORE_EXTENSION, new ClusterPropertiesJsonSerializer());
FileStore<ServiceProperties> fsServiceStore = new FileStore<>(testDirectory + File.separator + "service", FileSystemDirectory.FILE_STORE_EXTENSION, new ServicePropertiesJsonSerializer());
FileStore<UriProperties> fsUriStore = new FileStore<>(testDirectory + File.separator + "uri", FileSystemDirectory.FILE_STORE_EXTENSION, new UriPropertiesJsonSerializer());
// chains
PropertyEventThread thread = new PropertyEventThread("echo client event thread");
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("echo client event thread"));
// start up the world
thread.start();
PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<>(executorService, zkServiceRegistry);
serviceBus.register(fsServiceStore);
new ZooKeeperTogglingStore<>(zkServiceRegistry, fsServiceStore, serviceBus, true);
PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<>(executorService, zkUriRegistry);
uriBus.register(fsUriStore);
new ZooKeeperTogglingStore<>(zkUriRegistry, fsUriStore, uriBus, true);
PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<>(executorService, zkClusterRegistry);
clusterBus.register(fsClusterStore);
new ZooKeeperTogglingStore<>(zkClusterRegistry, fsClusterStore, clusterBus, true);
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<>();
// strategy and scheme factories
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
Map<String, TransportClientFactory> clientFactories = new HashMap<>();
clientFactories.put("http", new HttpClientFactory.Builder().build());
// create the state
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS, executorService);
new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state);
return balancer;
}
use of com.linkedin.r2.util.NamedThreadFactory in project rest.li by linkedin.
the class TestResponseCompression method testResponseCompression.
// Often fails in CI without a retry
@Test(dataProvider = "requestData", retryAnalyzer = SingleRetry.class)
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<>();
if (responseCompressionConfig != null) {
responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
}
HttpClientFactory httpClientFactory = new HttpClientFactory.Builder().setEventLoopGroup(new NioEventLoopGroup(0, /* use default settings */
new NamedThreadFactory("R2 Nio Event Loop"))).setShutDownFactory(true).setScheduleExecutorService(executor).setShutdownScheduledExecutorService(true).setCallbackExecutor(executor).setShutdownCallbackExecutor(false).setJmxManager(AbstractJmxManager.NULL_JMX_MANAGER).setRequestCompressionThresholdDefault(Integer.MAX_VALUE).setRequestCompressionConfigs(Collections.<String, CompressionConfig>emptyMap()).setResponseCompressionConfigs(responseCompressionConfigs).setUseClientCompression(true).build();
Map<String, Object> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
if (useResponseCompression != null) {
properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
}
TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
Long[] ids = new Long[idCount];
for (int i = 0; i < ids.length; i++) {
ids[i] = (long) i;
}
BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
if (expectedCompressionThreshold != null) {
builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
}
Request<BatchResponse<Greeting>> request = builder.build();
Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
if (responseShouldBeCompressed) {
Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
} else {
Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
}
}
Aggregations