14
14
import com .aws .greengrass .clientdevices .auth .configuration .CDAConfiguration ;
15
15
import com .aws .greengrass .clientdevices .auth .configuration .GroupConfiguration ;
16
16
import com .aws .greengrass .clientdevices .auth .configuration .GroupManager ;
17
+ import com .aws .greengrass .clientdevices .auth .configuration .InfrastructureConfiguration ;
17
18
import com .aws .greengrass .clientdevices .auth .connectivity .CISShadowMonitor ;
19
+ import com .aws .greengrass .clientdevices .auth .infra .CDAExecutor ;
18
20
import com .aws .greengrass .clientdevices .auth .infra .NetworkState ;
19
21
import com .aws .greengrass .clientdevices .auth .session .MqttSessionFactory ;
20
22
import com .aws .greengrass .clientdevices .auth .session .SessionConfig ;
30
32
import com .aws .greengrass .ipc .SubscribeToCertificateUpdatesOperationHandler ;
31
33
import com .aws .greengrass .ipc .VerifyClientDeviceIdentityOperationHandler ;
32
34
import com .aws .greengrass .lifecyclemanager .PluginService ;
33
- import com .aws .greengrass .util .Coerce ;
34
35
import com .fasterxml .jackson .databind .MapperFeature ;
35
36
import com .fasterxml .jackson .databind .ObjectMapper ;
36
37
import software .amazon .awssdk .aws .greengrass .GreengrassCoreIPCService ;
45
46
import java .util .concurrent .TimeUnit ;
46
47
import javax .inject .Inject ;
47
48
49
+ import static com .aws .greengrass .clientdevices .auth .configuration .InfrastructureConfiguration .DEFAULT_THREAD_POOL_SIZE ;
50
+ import static com .aws .greengrass .clientdevices .auth .configuration .InfrastructureConfiguration .DEFAULT_WORK_QUEUE_DEPTH ;
48
51
import static com .aws .greengrass .componentmanager .KernelConfigResolver .CONFIGURATION_CONFIG_KEY ;
49
52
import static software .amazon .awssdk .aws .greengrass .GreengrassCoreIPCService .AUTHORIZE_CLIENT_DEVICE_ACTION ;
50
53
import static software .amazon .awssdk .aws .greengrass .GreengrassCoreIPCService .GET_CLIENT_DEVICE_AUTH_TOKEN ;
55
58
public class ClientDevicesAuthService extends PluginService {
56
59
public static final String CLIENT_DEVICES_AUTH_SERVICE_NAME = "aws.greengrass.clientdevices.Auth" ;
57
60
61
+ private CDAConfiguration cdaConfiguration ;
62
+ private InfrastructureConfiguration infrastructureConfig ;
63
+
58
64
// TODO: Move configuration related constants to appropriate configuration class
59
65
public static final String DEVICE_GROUPS_TOPICS = "deviceGroups" ;
60
66
public static final String PERFORMANCE_TOPIC = "performance" ;
61
67
public static final String MAX_ACTIVE_AUTH_TOKENS_TOPIC = "maxActiveAuthTokens" ;
62
68
public static final String CLOUD_REQUEST_QUEUE_SIZE_TOPIC = "cloudRequestQueueSize" ;
63
69
public static final String MAX_CONCURRENT_CLOUD_REQUESTS_TOPIC = "maxConcurrentCloudRequests" ;
64
- // Limit the queue size before we start rejecting requests
65
- private static final int DEFAULT_CLOUD_CALL_QUEUE_SIZE = 100 ;
66
- private static final int DEFAULT_THREAD_POOL_SIZE = 1 ;
67
70
public static final int DEFAULT_MAX_ACTIVE_AUTH_TOKENS = 2500 ;
68
71
69
- // Create a threadpool for calling the cloud. Single thread will be used by default.
70
- private ThreadPoolExecutor cloudCallThreadPool ;
71
- private int cloudCallQueueSize ;
72
- private CDAConfiguration cdaConfiguration ;
73
-
74
72
75
73
/**
76
74
* Constructor.
@@ -88,32 +86,10 @@ protected void install() throws InterruptedException {
88
86
89
87
context .get (UseCases .class ).init (context );
90
88
context .get (CertificateManager .class ).updateCertificatesConfiguration (new CertificatesConfig (getConfig ()));
91
- initializeInfrastructure ();
92
89
initializeHandlers ();
93
90
subscribeToConfigChanges ();
94
91
}
95
92
96
- private int getValidCloudCallQueueSize (Topics topics ) {
97
- int newSize = Coerce .toInt (
98
- topics .findOrDefault (DEFAULT_CLOUD_CALL_QUEUE_SIZE ,
99
- CONFIGURATION_CONFIG_KEY , PERFORMANCE_TOPIC , CLOUD_REQUEST_QUEUE_SIZE_TOPIC ));
100
- if (newSize <= 0 ) {
101
- logger .atWarn ().log ("{} illegal size, will not change the queue size from {}" ,
102
- CLOUD_REQUEST_QUEUE_SIZE_TOPIC , cloudCallQueueSize );
103
- return cloudCallQueueSize ; // existing size
104
- }
105
- return newSize ;
106
- }
107
-
108
- private void initializeInfrastructure () {
109
- cloudCallQueueSize = DEFAULT_CLOUD_CALL_QUEUE_SIZE ;
110
- cloudCallQueueSize = getValidCloudCallQueueSize (config );
111
- cloudCallThreadPool = new ThreadPoolExecutor (1 ,
112
- DEFAULT_THREAD_POOL_SIZE , 60 , TimeUnit .SECONDS ,
113
- new ResizableLinkedBlockingQueue <>(cloudCallQueueSize ));
114
- cloudCallThreadPool .allowCoreThreadTimeOut (true ); // act as a cached threadpool
115
- }
116
-
117
93
private void initializeHandlers () {
118
94
// Register auth session handlers
119
95
context .get (SessionManager .class ).setSessionConfig (new SessionConfig (getConfig ()));
@@ -146,34 +122,20 @@ private void configChangeHandler(WhatHappened whatHappened, Node node) {
146
122
return ;
147
123
}
148
124
logger .atDebug ().kv ("why" , whatHappened ).kv ("node" , node ).log ();
125
+
149
126
// NOTE: This should not live here. The service doesn't have to have knowledge about where/how
150
127
// keys are stored
151
- Topics deviceGroupTopics = this .config .lookupTopics (CONFIGURATION_CONFIG_KEY , DEVICE_GROUPS_TOPICS );
152
-
153
- try {
154
- // NOTE: Extract this to a method these are infrastructure concerns.
155
- int threadPoolSize = Coerce .toInt (this .config .findOrDefault (DEFAULT_THREAD_POOL_SIZE ,
156
- CONFIGURATION_CONFIG_KEY , PERFORMANCE_TOPIC , MAX_CONCURRENT_CLOUD_REQUESTS_TOPIC ));
157
- if (threadPoolSize >= cloudCallThreadPool .getCorePoolSize ()) {
158
- cloudCallThreadPool .setMaximumPoolSize (threadPoolSize );
159
- }
160
- } catch (IllegalArgumentException e ) {
161
- logger .atWarn ().log ("Unable to update CDA threadpool size due to {}" , e .getMessage ());
162
- }
163
-
164
- if (whatHappened != WhatHappened .initialized && node != null && node .childOf (CLOUD_REQUEST_QUEUE_SIZE_TOPIC )) {
165
- // NOTE: Extract this to a method these are infrastructure concerns.
166
- BlockingQueue <Runnable > q = cloudCallThreadPool .getQueue ();
167
- if (q instanceof ResizableLinkedBlockingQueue ) {
168
- cloudCallQueueSize = getValidCloudCallQueueSize (this .config );
169
- ((ResizableLinkedBlockingQueue ) q ).resize (cloudCallQueueSize );
170
- }
171
- }
172
-
173
128
if (whatHappened == WhatHappened .initialized || node == null || node .childOf (DEVICE_GROUPS_TOPICS )) {
129
+ Topics deviceGroupTopics = this .config .lookupTopics (CONFIGURATION_CONFIG_KEY , DEVICE_GROUPS_TOPICS );
174
130
updateDeviceGroups (whatHappened , deviceGroupTopics );
175
131
}
176
132
133
+ InfrastructureConfiguration newInfraConfig = InfrastructureConfiguration .from (getConfig ());
134
+ if (infrastructureConfig == null || !newInfraConfig .equals (infrastructureConfig )) {
135
+ updateInfrastructure (newInfraConfig );
136
+ infrastructureConfig = newInfraConfig ;
137
+ }
138
+
177
139
onConfigurationChanged ();
178
140
}
179
141
@@ -189,10 +151,20 @@ protected void shutdown() throws InterruptedException {
189
151
context .get (CertificateManager .class ).stopMonitors ();
190
152
}
191
153
192
- @ Override
193
- public void postInject () {
194
- super .postInject ();
154
+ private void updateInfrastructure (InfrastructureConfiguration infraConfig ) {
155
+ context .get (CDAExecutor .class ).accept (infraConfig );
156
+ }
157
+
158
+ private void initializeInfrastructure () {
159
+ BlockingQueue <Runnable > queue = new ResizableLinkedBlockingQueue <>(DEFAULT_WORK_QUEUE_DEPTH );
160
+ ThreadPoolExecutor executor = new ThreadPoolExecutor (DEFAULT_THREAD_POOL_SIZE ,
161
+ DEFAULT_THREAD_POOL_SIZE , 60 , TimeUnit .SECONDS , queue );
162
+ context .put (CDAExecutor .class , new CDAExecutor (executor ));
163
+ }
164
+
165
+ private void initializeIPC () {
195
166
AuthorizationHandler authorizationHandler = context .get (AuthorizationHandler .class );
167
+
196
168
try {
197
169
authorizationHandler .registerComponent (this .getName (),
198
170
new HashSet <>(Arrays .asList (SUBSCRIBE_TO_CERTIFICATE_UPDATES ,
@@ -212,17 +184,20 @@ public void postInject() {
212
184
new SubscribeToCertificateUpdatesOperationHandler (context , certificateManager , authorizationHandler ));
213
185
greengrassCoreIPCService .setVerifyClientDeviceIdentityHandler (context ->
214
186
new VerifyClientDeviceIdentityOperationHandler (context , serviceApi ,
215
- authorizationHandler , cloudCallThreadPool ));
187
+ authorizationHandler , this . context . get ( CDAExecutor . class ) ));
216
188
greengrassCoreIPCService .setGetClientDeviceAuthTokenHandler (context ->
217
189
new GetClientDeviceAuthTokenOperationHandler (context , serviceApi , authorizationHandler ,
218
- cloudCallThreadPool ));
190
+ this . context . get ( CDAExecutor . class ) ));
219
191
greengrassCoreIPCService .setAuthorizeClientDeviceActionHandler (context ->
220
192
new AuthorizeClientDeviceActionOperationHandler (context , serviceApi ,
221
193
authorizationHandler ));
222
194
}
223
195
224
- public CertificateManager getCertificateManager () {
225
- return context .get (CertificateManager .class );
196
+ @ Override
197
+ public void postInject () {
198
+ super .postInject ();
199
+ initializeInfrastructure ();
200
+ initializeIPC ();
226
201
}
227
202
228
203
private void updateDeviceGroups (WhatHappened whatHappened , Topics deviceGroupsTopics ) {
@@ -250,7 +225,7 @@ void updateCACertificateConfig(List<String> caCerts) {
250
225
protected CompletableFuture <Void > close (boolean waitForDependers ) {
251
226
// shutdown the threadpool in close, not in shutdown() because it is created
252
227
// and injected in the constructor and we won't be able to restart it after it stops.
253
- cloudCallThreadPool .shutdown ();
228
+ context . get ( CDAExecutor . class ) .shutdown ();
254
229
return super .close (waitForDependers );
255
230
}
256
231
}
0 commit comments