/
ClientExecutionServiceImpl.java
134 lines (116 loc) · 5.59 KB
/
ClientExecutionServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.client.spi.impl;
import com.hazelcast.client.spi.ClientExecutionService;
import com.hazelcast.internal.metrics.MetricsProvider;
import com.hazelcast.internal.metrics.MetricsRegistry;
import com.hazelcast.internal.metrics.Probe;
import com.hazelcast.internal.util.RuntimeAvailableProcessors;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.LoggingService;
import com.hazelcast.spi.properties.HazelcastProperties;
import com.hazelcast.spi.properties.HazelcastProperty;
import com.hazelcast.util.executor.LoggingScheduledExecutor;
import com.hazelcast.util.executor.PoolExecutorThreadFactory;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static com.hazelcast.internal.metrics.ProbeLevel.MANDATORY;
import static java.lang.Thread.currentThread;
public final class ClientExecutionServiceImpl implements ClientExecutionService, MetricsProvider {
public static final HazelcastProperty INTERNAL_EXECUTOR_POOL_SIZE
= new HazelcastProperty("hazelcast.client.internal.executor.pool.size", 3);
public static final long TERMINATE_TIMEOUT_SECONDS = 30;
private final ILogger logger;
private final ExecutorService userExecutor;
private final ScheduledExecutorService internalExecutor;
public ClientExecutionServiceImpl(String name, ClassLoader classLoader,
HazelcastProperties properties, int poolSize, LoggingService loggingService) {
int internalPoolSize = properties.getInteger(INTERNAL_EXECUTOR_POOL_SIZE);
if (internalPoolSize <= 0) {
internalPoolSize = Integer.parseInt(INTERNAL_EXECUTOR_POOL_SIZE.getDefaultValue());
}
int executorPoolSize = poolSize;
if (executorPoolSize <= 0) {
executorPoolSize = RuntimeAvailableProcessors.get();
}
logger = loggingService.getLogger(ClientExecutionService.class);
internalExecutor = new LoggingScheduledExecutor(logger, internalPoolSize,
new PoolExecutorThreadFactory(name + ".internal-", classLoader), (r, executor) -> {
String message = "Internal executor rejected task: " + r + ", because client is shutting down...";
logger.finest(message);
throw new RejectedExecutionException(message);
});
userExecutor = new ThreadPoolExecutor(executorPoolSize, executorPoolSize, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(),
new PoolExecutorThreadFactory(name + ".user-", classLoader), (r, executor) -> {
String message = "User executor rejected task: " + r + ", because client is shutting down...";
logger.finest(message);
throw new RejectedExecutionException(message);
});
}
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
return internalExecutor.schedule(command, delay, unit);
}
@Override
public <V> ScheduledFuture<Future<V>> schedule(Callable<V> command, long delay, TimeUnit unit) {
return (ScheduledFuture<Future<V>>) internalExecutor.schedule(command, delay, unit);
}
@Override
public ScheduledFuture<?> scheduleWithRepetition(Runnable command, long initialDelay, long period, TimeUnit unit) {
return internalExecutor.scheduleAtFixedRate(command, initialDelay, period, unit);
}
@Override
public void execute(Runnable command) {
internalExecutor.execute(command);
}
@Override
public ExecutorService getUserExecutor() {
return userExecutor;
}
@Probe (level = MANDATORY)
public int getUserExecutorQueueSize() {
return ((ThreadPoolExecutor) userExecutor).getQueue().size();
}
public void shutdown() {
shutdownExecutor("user", userExecutor, logger);
shutdownExecutor("internal", internalExecutor, logger);
}
public static void shutdownExecutor(String name, ExecutorService executor, ILogger logger) {
executor.shutdown();
try {
boolean success = executor.awaitTermination(TERMINATE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (!success) {
logger.warning(name + " executor awaitTermination could not complete in " + TERMINATE_TIMEOUT_SECONDS
+ " seconds");
}
} catch (InterruptedException e) {
currentThread().interrupt();
logger.warning(name + " executor await termination is interrupted", e);
}
}
@Override
public void provideMetrics(MetricsRegistry registry) {
registry.scanAndRegister(this, "executionService");
}
}