-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathPluginSettings.kt
486 lines (426 loc) · 17.2 KB
/
PluginSettings.kt
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.observability.settings
import org.apache.logging.log4j.LogManager
import org.opensearch.bootstrap.BootstrapInfo
import org.opensearch.cluster.service.ClusterService
import org.opensearch.common.settings.Setting
import org.opensearch.common.settings.Setting.Property.Dynamic
import org.opensearch.common.settings.Setting.Property.NodeScope
import org.opensearch.common.settings.Settings
import org.opensearch.observability.ObservabilityPlugin.Companion.LOG_PREFIX
import org.opensearch.observability.ObservabilityPlugin.Companion.PLUGIN_NAME
import java.io.IOException
import java.nio.file.Path
/**
* settings specific to observability Plugin.
*/
internal object PluginSettings {
/**
* Settings Key prefix for this plugin.
*/
private const val KEY_PREFIX = "opensearch.observability"
/**
* General settings Key prefix.
*/
private const val GENERAL_KEY_PREFIX = "$KEY_PREFIX.general"
/**
* Polling settings Key prefix.
*/
private const val POLLING_KEY_PREFIX = "$KEY_PREFIX.polling"
/**
* Access settings Key prefix.
*/
private const val ACCESS_KEY_PREFIX = "$KEY_PREFIX.access"
/**
* Operation timeout for network operations.
*/
private const val OPERATION_TIMEOUT_MS_KEY = "$GENERAL_KEY_PREFIX.operationTimeoutMs"
/**
* Setting to choose Job lock duration.
*/
private const val JOB_LOCK_DURATION_S_KEY = "$POLLING_KEY_PREFIX.jobLockDurationSeconds"
/**
* Setting to choose Minimum polling duration.
*/
private const val MIN_POLLING_DURATION_S_KEY = "$POLLING_KEY_PREFIX.minPollingDurationSeconds"
/**
* Setting to choose Maximum polling duration.
*/
private const val MAX_POLLING_DURATION_S_KEY = "$POLLING_KEY_PREFIX.maxPollingDurationSeconds"
/**
* Setting to choose Maximum number of retries to try locking.
*/
private const val MAX_LOCK_RETRIES_KEY = "$POLLING_KEY_PREFIX.maxLockRetries"
/**
* Setting to choose default number of items to query.
*/
private const val DEFAULT_ITEMS_QUERY_COUNT_KEY = "$GENERAL_KEY_PREFIX.defaultItemsQueryCount"
/**
* Setting to choose admin access restriction.
*/
private const val ADMIN_ACCESS_KEY = "$ACCESS_KEY_PREFIX.adminAccess"
/**
* Setting to choose filter method.
*/
private const val FILTER_BY_KEY = "$ACCESS_KEY_PREFIX.filterBy"
/**
* Setting to choose ignored roles for filtering.
*/
private const val IGNORE_ROLE_KEY = "$ACCESS_KEY_PREFIX.ignoreRoles"
/**
* Default operation timeout for network operations.
*/
private const val DEFAULT_OPERATION_TIMEOUT_MS = 60000L
/**
* Minimum operation timeout for network operations.
*/
private const val MINIMUM_OPERATION_TIMEOUT_MS = 100L
/**
* Default Job lock duration.
*/
private const val DEFAULT_JOB_LOCK_DURATION_S = 300
/**
* Minimum Job lock duration.
*/
private const val MINIMUM_JOB_LOCK_DURATION_S = 10
/**
* Default Minimum polling duration.
*/
private const val DEFAULT_MIN_POLLING_DURATION_S = 300
/**
* Minimum Min polling duration.
*/
private const val MINIMUM_MIN_POLLING_DURATION_S = 60
/**
* Default Maximum polling duration.
*/
private const val DEFAULT_MAX_POLLING_DURATION_S = 900
/**
* Minimum Maximum polling duration.
*/
private const val MINIMUM_MAX_POLLING_DURATION_S = 300
/**
* Default number of retries to try locking.
*/
private const val DEFAULT_MAX_LOCK_RETRIES = 4
/**
* Minimum number of retries to try locking.
*/
private const val MINIMUM_LOCK_RETRIES = 1
/**
* Default number of items to query.
*/
private const val DEFAULT_ITEMS_QUERY_COUNT_VALUE = 1000
/**
* Minimum number of items to query.
*/
private const val MINIMUM_ITEMS_QUERY_COUNT = 10
/**
* Default admin access method.
*/
private const val DEFAULT_ADMIN_ACCESS_METHOD = "AllObservabilityObjects"
/**
* Default filter-by method.
*/
private const val DEFAULT_FILTER_BY_METHOD = "NoFilter"
/**
* Default filter-by method.
*/
private val DEFAULT_IGNORED_ROLES = listOf(
"own_index",
"opensearch_dashboards_user",
"notebooks_full_access",
"notebooks_read_access"
)
/**
* Operation timeout setting in ms for I/O operations
*/
@Volatile
var operationTimeoutMs: Long
/**
* Job lock duration
*/
@Volatile
var jobLockDurationSeconds: Int
/**
* Minimum polling duration
*/
@Volatile
var minPollingDurationSeconds: Int
/**
* Maximum polling duration.
*/
@Volatile
var maxPollingDurationSeconds: Int
/**
* Max number of retries to try locking.
*/
@Volatile
var maxLockRetries: Int
/**
* Default number of items to query.
*/
@Volatile
var defaultItemsQueryCount: Int
/**
* admin access method.
*/
@Volatile
var adminAccess: AdminAccess
/**
* Filter-by method.
*/
@Volatile
var filterBy: FilterBy
/**
* list of ignored roles.
*/
@Volatile
var ignoredRoles: List<String>
/**
* Enum for types of admin access
* "Standard" -> Admin user access follows standard user
* "AllObservabilityObjects" -> Admin user with "all_access" role can see all observability objects of all users.
*/
internal enum class AdminAccess { Standard, AllObservabilityObjects }
/**
* Enum for types of filterBy options
* NoFilter -> everyone see each other's notebooks
* User -> notebooks are visible to only themselves
* Roles -> notebooks are visible to users having any one of the role of creator
* BackendRoles -> notebooks are visible to users having any one of the backend role of creator
*/
internal enum class FilterBy { NoFilter, User, Roles, BackendRoles }
private const val DECIMAL_RADIX: Int = 10
private val log = LogManager.getLogger(javaClass)
private val defaultSettings: Map<String, String>
init {
var settings: Settings? = null
val configDirName = BootstrapInfo.getSystemProperties()?.get("opensearch.path.conf")?.toString()
if (configDirName != null) {
val defaultSettingYmlFile = Path.of(configDirName, PLUGIN_NAME, "observability.yml")
try {
settings = Settings.builder().loadFromPath(defaultSettingYmlFile).build()
} catch (exception: IOException) {
log.warn("$LOG_PREFIX:Failed to load ${defaultSettingYmlFile.toAbsolutePath()} message:${exception.message}")
}
}
// Initialize the settings values to default values
operationTimeoutMs = (settings?.get(OPERATION_TIMEOUT_MS_KEY)?.toLong()) ?: DEFAULT_OPERATION_TIMEOUT_MS
jobLockDurationSeconds = (settings?.get(JOB_LOCK_DURATION_S_KEY)?.toInt()) ?: DEFAULT_JOB_LOCK_DURATION_S
minPollingDurationSeconds = (settings?.get(MIN_POLLING_DURATION_S_KEY)?.toInt())
?: DEFAULT_MIN_POLLING_DURATION_S
maxPollingDurationSeconds = (settings?.get(MAX_POLLING_DURATION_S_KEY)?.toInt())
?: DEFAULT_MAX_POLLING_DURATION_S
maxLockRetries = (settings?.get(MAX_LOCK_RETRIES_KEY)?.toInt()) ?: DEFAULT_MAX_LOCK_RETRIES
defaultItemsQueryCount = (settings?.get(DEFAULT_ITEMS_QUERY_COUNT_KEY)?.toInt())
?: DEFAULT_ITEMS_QUERY_COUNT_VALUE
adminAccess = AdminAccess.valueOf(settings?.get(ADMIN_ACCESS_KEY) ?: DEFAULT_ADMIN_ACCESS_METHOD)
filterBy = FilterBy.valueOf(settings?.get(FILTER_BY_KEY) ?: DEFAULT_FILTER_BY_METHOD)
ignoredRoles = settings?.getAsList(IGNORE_ROLE_KEY) ?: DEFAULT_IGNORED_ROLES
defaultSettings = mapOf(
OPERATION_TIMEOUT_MS_KEY to operationTimeoutMs.toString(DECIMAL_RADIX),
JOB_LOCK_DURATION_S_KEY to jobLockDurationSeconds.toString(DECIMAL_RADIX),
MIN_POLLING_DURATION_S_KEY to minPollingDurationSeconds.toString(DECIMAL_RADIX),
MAX_POLLING_DURATION_S_KEY to maxPollingDurationSeconds.toString(DECIMAL_RADIX),
MAX_LOCK_RETRIES_KEY to maxLockRetries.toString(DECIMAL_RADIX),
DEFAULT_ITEMS_QUERY_COUNT_KEY to defaultItemsQueryCount.toString(DECIMAL_RADIX),
ADMIN_ACCESS_KEY to adminAccess.name,
FILTER_BY_KEY to filterBy.name
)
}
private val OPERATION_TIMEOUT_MS: Setting<Long> = Setting.longSetting(
OPERATION_TIMEOUT_MS_KEY,
defaultSettings[OPERATION_TIMEOUT_MS_KEY]!!.toLong(),
MINIMUM_OPERATION_TIMEOUT_MS,
NodeScope,
Dynamic
)
private val JOB_LOCK_DURATION_S: Setting<Int> = Setting.intSetting(
JOB_LOCK_DURATION_S_KEY,
defaultSettings[JOB_LOCK_DURATION_S_KEY]!!.toInt(),
MINIMUM_JOB_LOCK_DURATION_S,
NodeScope,
Dynamic
)
private val MIN_POLLING_DURATION_S: Setting<Int> = Setting.intSetting(
MIN_POLLING_DURATION_S_KEY,
defaultSettings[MIN_POLLING_DURATION_S_KEY]!!.toInt(),
MINIMUM_MIN_POLLING_DURATION_S,
NodeScope,
Dynamic
)
private val MAX_POLLING_DURATION_S: Setting<Int> = Setting.intSetting(
MAX_POLLING_DURATION_S_KEY,
defaultSettings[MAX_POLLING_DURATION_S_KEY]!!.toInt(),
MINIMUM_MAX_POLLING_DURATION_S,
NodeScope,
Dynamic
)
private val MAX_LOCK_RETRIES: Setting<Int> = Setting.intSetting(
MAX_LOCK_RETRIES_KEY,
defaultSettings[MAX_LOCK_RETRIES_KEY]!!.toInt(),
MINIMUM_LOCK_RETRIES,
NodeScope,
Dynamic
)
private val DEFAULT_ITEMS_QUERY_COUNT: Setting<Int> = Setting.intSetting(
DEFAULT_ITEMS_QUERY_COUNT_KEY,
defaultSettings[DEFAULT_ITEMS_QUERY_COUNT_KEY]!!.toInt(),
MINIMUM_ITEMS_QUERY_COUNT,
NodeScope,
Dynamic
)
private val ADMIN_ACCESS: Setting<String> = Setting.simpleString(
ADMIN_ACCESS_KEY,
defaultSettings[ADMIN_ACCESS_KEY]!!,
NodeScope,
Dynamic
)
private val FILTER_BY: Setting<String> = Setting.simpleString(
FILTER_BY_KEY,
defaultSettings[FILTER_BY_KEY]!!,
NodeScope,
Dynamic
)
private val IGNORED_ROLES: Setting<List<String>> = Setting.listSetting(
IGNORE_ROLE_KEY,
DEFAULT_IGNORED_ROLES,
{ it },
NodeScope,
Dynamic
)
/**
* Returns list of additional settings available specific to this plugin.
*
* @return list of settings defined in this plugin
*/
fun getAllSettings(): List<Setting<*>> {
return listOf(
OPERATION_TIMEOUT_MS,
JOB_LOCK_DURATION_S,
MIN_POLLING_DURATION_S,
MAX_POLLING_DURATION_S,
MAX_LOCK_RETRIES,
DEFAULT_ITEMS_QUERY_COUNT,
ADMIN_ACCESS,
FILTER_BY,
IGNORED_ROLES
)
}
/**
* Update the setting variables to setting values from local settings
* @param clusterService cluster service instance
*/
private fun updateSettingValuesFromLocal(clusterService: ClusterService) {
operationTimeoutMs = OPERATION_TIMEOUT_MS.get(clusterService.settings)
jobLockDurationSeconds = JOB_LOCK_DURATION_S.get(clusterService.settings)
minPollingDurationSeconds = MIN_POLLING_DURATION_S.get(clusterService.settings)
maxPollingDurationSeconds = MAX_POLLING_DURATION_S.get(clusterService.settings)
maxLockRetries = MAX_LOCK_RETRIES.get(clusterService.settings)
defaultItemsQueryCount = DEFAULT_ITEMS_QUERY_COUNT.get(clusterService.settings)
adminAccess = AdminAccess.valueOf(ADMIN_ACCESS.get(clusterService.settings))
filterBy = FilterBy.valueOf(FILTER_BY.get(clusterService.settings))
ignoredRoles = IGNORED_ROLES.get(clusterService.settings)
}
/**
* Update the setting variables to setting values from cluster settings
* @param clusterService cluster service instance
*/
private fun updateSettingValuesFromCluster(clusterService: ClusterService) {
val clusterOperationTimeoutMs = clusterService.clusterSettings.get(OPERATION_TIMEOUT_MS)
if (clusterOperationTimeoutMs != null) {
log.debug("$LOG_PREFIX:$OPERATION_TIMEOUT_MS_KEY -autoUpdatedTo-> $clusterOperationTimeoutMs")
operationTimeoutMs = clusterOperationTimeoutMs
}
val clusterJobLockDurationSeconds = clusterService.clusterSettings.get(JOB_LOCK_DURATION_S)
if (clusterJobLockDurationSeconds != null) {
log.debug("$LOG_PREFIX:$JOB_LOCK_DURATION_S_KEY -autoUpdatedTo-> $clusterJobLockDurationSeconds")
jobLockDurationSeconds = clusterJobLockDurationSeconds
}
val clusterMinPollingDurationSeconds = clusterService.clusterSettings.get(MIN_POLLING_DURATION_S)
if (clusterMinPollingDurationSeconds != null) {
log.debug("$LOG_PREFIX:$MIN_POLLING_DURATION_S_KEY -autoUpdatedTo-> $clusterMinPollingDurationSeconds")
minPollingDurationSeconds = clusterMinPollingDurationSeconds
}
val clusterMaxPollingDurationSeconds = clusterService.clusterSettings.get(MAX_POLLING_DURATION_S)
if (clusterMaxPollingDurationSeconds != null) {
log.debug("$LOG_PREFIX:$MAX_POLLING_DURATION_S_KEY -autoUpdatedTo-> $clusterMaxPollingDurationSeconds")
maxPollingDurationSeconds = clusterMaxPollingDurationSeconds
}
val clusterMaxLockRetries = clusterService.clusterSettings.get(MAX_LOCK_RETRIES)
if (clusterMaxLockRetries != null) {
log.debug("$LOG_PREFIX:$MAX_LOCK_RETRIES_KEY -autoUpdatedTo-> $clusterMaxLockRetries")
maxLockRetries = clusterMaxLockRetries
}
val clusterDefaultItemsQueryCount = clusterService.clusterSettings.get(DEFAULT_ITEMS_QUERY_COUNT)
if (clusterDefaultItemsQueryCount != null) {
log.debug("$LOG_PREFIX:$DEFAULT_ITEMS_QUERY_COUNT_KEY -autoUpdatedTo-> $clusterDefaultItemsQueryCount")
defaultItemsQueryCount = clusterDefaultItemsQueryCount
}
val clusterAdminAccess = clusterService.clusterSettings.get(ADMIN_ACCESS)
if (clusterAdminAccess != null) {
log.debug("$LOG_PREFIX:$ADMIN_ACCESS_KEY -autoUpdatedTo-> $clusterAdminAccess")
adminAccess = AdminAccess.valueOf(clusterAdminAccess)
}
val clusterFilterBy = clusterService.clusterSettings.get(FILTER_BY)
if (clusterFilterBy != null) {
log.debug("$LOG_PREFIX:$FILTER_BY_KEY -autoUpdatedTo-> $clusterFilterBy")
filterBy = FilterBy.valueOf(clusterFilterBy)
}
val clusterIgnoredRoles = clusterService.clusterSettings.get(IGNORED_ROLES)
if (clusterIgnoredRoles != null) {
log.debug("$LOG_PREFIX:$IGNORE_ROLE_KEY -autoUpdatedTo-> $clusterIgnoredRoles")
ignoredRoles = clusterIgnoredRoles
}
}
/**
* adds Settings update listeners to all settings.
* @param clusterService cluster service instance
*/
fun addSettingsUpdateConsumer(clusterService: ClusterService) {
updateSettingValuesFromLocal(clusterService)
// Update the variables to cluster setting values
// If the cluster is not yet started then we get default values again
updateSettingValuesFromCluster(clusterService)
clusterService.clusterSettings.addSettingsUpdateConsumer(OPERATION_TIMEOUT_MS) {
operationTimeoutMs = it
log.info("$LOG_PREFIX:$OPERATION_TIMEOUT_MS_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(JOB_LOCK_DURATION_S) {
jobLockDurationSeconds = it
log.info("$LOG_PREFIX:$JOB_LOCK_DURATION_S_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(MIN_POLLING_DURATION_S) {
minPollingDurationSeconds = it
log.info("$LOG_PREFIX:$MIN_POLLING_DURATION_S_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(MAX_POLLING_DURATION_S) {
maxPollingDurationSeconds = it
log.info("$LOG_PREFIX:$MAX_POLLING_DURATION_S_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(MAX_LOCK_RETRIES) {
maxLockRetries = it
log.info("$LOG_PREFIX:$MAX_LOCK_RETRIES_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(DEFAULT_ITEMS_QUERY_COUNT) {
defaultItemsQueryCount = it
log.info("$LOG_PREFIX:$DEFAULT_ITEMS_QUERY_COUNT_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(ADMIN_ACCESS) {
adminAccess = AdminAccess.valueOf(it)
log.info("$LOG_PREFIX:$ADMIN_ACCESS_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(FILTER_BY) {
filterBy = FilterBy.valueOf(it)
log.info("$LOG_PREFIX:$FILTER_BY_KEY -updatedTo-> $it")
}
clusterService.clusterSettings.addSettingsUpdateConsumer(IGNORED_ROLES) {
ignoredRoles = it
log.info("$LOG_PREFIX:$IGNORE_ROLE_KEY -updatedTo-> $it")
}
}
}