-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
PersistenceManagerImpl.java
500 lines (463 loc) · 21.6 KB
/
PersistenceManagerImpl.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.persistence.internal;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.NamedThreadFactory;
import org.openhab.core.common.SafeCaller;
import org.openhab.core.items.GenericItem;
import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.items.ItemRegistryChangeListener;
import org.openhab.core.items.StateChangeListener;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.persistence.PersistenceItemConfiguration;
import org.openhab.core.persistence.PersistenceManager;
import org.openhab.core.persistence.PersistenceService;
import org.openhab.core.persistence.PersistenceServiceConfiguration;
import org.openhab.core.persistence.QueryablePersistenceService;
import org.openhab.core.persistence.config.PersistenceAllConfig;
import org.openhab.core.persistence.config.PersistenceConfig;
import org.openhab.core.persistence.config.PersistenceGroupConfig;
import org.openhab.core.persistence.config.PersistenceItemConfig;
import org.openhab.core.persistence.strategy.PersistenceCronStrategy;
import org.openhab.core.persistence.strategy.PersistenceStrategy;
import org.openhab.core.scheduler.CronScheduler;
import org.openhab.core.scheduler.ScheduledCompletableFuture;
import org.openhab.core.service.ReadyMarker;
import org.openhab.core.service.ReadyMarkerFilter;
import org.openhab.core.service.ReadyService;
import org.openhab.core.service.ReadyService.ReadyTracker;
import org.openhab.core.service.StartLevelService;
import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class implements a persistence manager to manage all persistence services etc.
*
* @author Kai Kreuzer - Initial contribution
* @author Markus Rathgeb - Separation of persistence core and model, drop Quartz usage.
*/
@Component(immediate = true, service = PersistenceManager.class)
@NonNullByDefault
public class PersistenceManagerImpl
implements ItemRegistryChangeListener, PersistenceManager, StateChangeListener, ReadyTracker {
private final Logger logger = LoggerFactory.getLogger(PersistenceManagerImpl.class);
private final ReadyMarker marker = new ReadyMarker("persistence", "restore");
// the scheduler used for timer events
private final CronScheduler scheduler;
private final ItemRegistry itemRegistry;
private final SafeCaller safeCaller;
private final ReadyService readyService;
private volatile boolean started = false;
final Map<String, PersistenceService> persistenceServices = new HashMap<>();
final Map<String, @Nullable PersistenceServiceConfiguration> persistenceServiceConfigs = new HashMap<>();
private final Map<String, Set<ScheduledCompletableFuture<?>>> persistenceJobs = new HashMap<>();
@Activate
public PersistenceManagerImpl(final @Reference CronScheduler scheduler, final @Reference ItemRegistry itemRegistry,
final @Reference SafeCaller safeCaller, final @Reference ReadyService readyService) {
this.scheduler = scheduler;
this.itemRegistry = itemRegistry;
this.safeCaller = safeCaller;
this.readyService = readyService;
}
@Activate
protected void activate() {
readyService.registerTracker(this, new ReadyMarkerFilter().withType(StartLevelService.STARTLEVEL_MARKER_TYPE)
.withIdentifier(Integer.toString(StartLevelService.STARTLEVEL_MODEL)));
}
@Deactivate
protected void deactivate() {
itemRegistry.removeRegistryChangeListener(this);
started = false;
removeTimers();
removeItemStateChangeListeners();
}
@Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
protected void addPersistenceService(PersistenceService persistenceService) {
logger.debug("Initializing {} persistence service.", persistenceService.getId());
persistenceServices.put(persistenceService.getId(), persistenceService);
persistenceServiceConfigs.putIfAbsent(persistenceService.getId(), getDefaultConfig(persistenceService));
if (started) {
stopEventHandling(persistenceService.getId());
startEventHandling(persistenceService.getId());
}
}
protected void removePersistenceService(PersistenceService persistenceService) {
stopEventHandling(persistenceService.getId());
persistenceServices.remove(persistenceService.getId());
}
/**
* Calls all persistence services which use change or update policy for the given item
*
* @param item the item to persist
* @param onlyChanges true, if it has the change strategy, false otherwise
*/
private void handleStateEvent(Item item, boolean onlyChanges) {
synchronized (persistenceServiceConfigs) {
for (Entry<String, @Nullable PersistenceServiceConfiguration> entry : persistenceServiceConfigs
.entrySet()) {
final String serviceName = entry.getKey();
final PersistenceServiceConfiguration config = entry.getValue();
if (config != null && persistenceServices.containsKey(serviceName)) {
for (PersistenceItemConfiguration itemConfig : config.getConfigs()) {
if (hasStrategy(config, itemConfig, onlyChanges ? PersistenceStrategy.Globals.CHANGE
: PersistenceStrategy.Globals.UPDATE)) {
if (appliesToItem(itemConfig, item)) {
persistenceServices.get(serviceName).store(item, itemConfig.getAlias());
}
}
}
}
}
}
}
/**
* Checks if a given persistence configuration entry has a certain strategy for the given service
*
* @param config the configuration to check for
* @param itemConfig the persistence configuration entry
* @param strategy the strategy to check for
* @return true, if it has the given strategy
*/
private boolean hasStrategy(PersistenceServiceConfiguration config, PersistenceItemConfiguration itemConfig,
PersistenceStrategy strategy) {
if (config.getDefaults().contains(strategy) && itemConfig.getStrategies().isEmpty()) {
return true;
} else {
for (PersistenceStrategy s : itemConfig.getStrategies()) {
if (s.equals(strategy)) {
return true;
}
}
return false;
}
}
/**
* Checks if a given persistence configuration entry is relevant for an item
*
* @param config the persistence configuration entry
* @param item to check if the configuration applies to
* @return true, if the configuration applies to the item
*/
private boolean appliesToItem(PersistenceItemConfiguration config, Item item) {
for (PersistenceConfig itemCfg : config.getItems()) {
if (itemCfg instanceof PersistenceAllConfig) {
return true;
}
if (itemCfg instanceof PersistenceItemConfig) {
PersistenceItemConfig singleItemConfig = (PersistenceItemConfig) itemCfg;
if (item.getName().equals(singleItemConfig.getItem())) {
return true;
}
}
if (itemCfg instanceof PersistenceGroupConfig) {
PersistenceGroupConfig groupItemConfig = (PersistenceGroupConfig) itemCfg;
try {
Item gItem = itemRegistry.getItem(groupItemConfig.getGroup());
if (gItem instanceof GroupItem) {
GroupItem groupItem = (GroupItem) gItem;
if (groupItem.getAllMembers().contains(item)) {
return true;
}
}
} catch (ItemNotFoundException e) {
// do nothing
}
}
}
return false;
}
/**
* Retrieves all items for which the persistence configuration applies to.
*
* @param config the persistence configuration entry
* @return all items that this configuration applies to
*/
Iterable<Item> getAllItems(PersistenceItemConfiguration config) {
// first check, if we should return them all
for (Object itemCfg : config.getItems()) {
if (itemCfg instanceof PersistenceAllConfig) {
return itemRegistry.getItems();
}
}
// otherwise, go through the detailed definitions
Set<Item> items = new HashSet<>();
for (Object itemCfg : config.getItems()) {
if (itemCfg instanceof PersistenceItemConfig) {
PersistenceItemConfig singleItemConfig = (PersistenceItemConfig) itemCfg;
String itemName = singleItemConfig.getItem();
try {
items.add(itemRegistry.getItem(itemName));
} catch (ItemNotFoundException e) {
logger.debug("Item '{}' does not exist.", itemName);
}
}
if (itemCfg instanceof PersistenceGroupConfig) {
PersistenceGroupConfig groupItemConfig = (PersistenceGroupConfig) itemCfg;
String groupName = groupItemConfig.getGroup();
try {
Item gItem = itemRegistry.getItem(groupName);
if (gItem instanceof GroupItem) {
GroupItem groupItem = (GroupItem) gItem;
items.addAll(groupItem.getAllMembers());
}
} catch (ItemNotFoundException e) {
logger.debug("Item group '{}' does not exist.", groupName);
}
}
}
return items;
}
/**
* Handles the "restoreOnStartup" strategy for the item.
* If the item state is still undefined when entering this method, all persistence configurations are checked,
* if they have the "restoreOnStartup" strategy configured for the item. If so, the item state will be set
* to its last persisted value.
*
* @param item the item to restore the state for
*/
@SuppressWarnings("null")
private void initialize(Item item) {
// get the last persisted state from the persistence service if no state is yet set
if (UnDefType.NULL.equals(item.getState()) && item instanceof GenericItem) {
for (Entry<String, @Nullable PersistenceServiceConfiguration> entry : persistenceServiceConfigs
.entrySet()) {
final String serviceName = entry.getKey();
final PersistenceServiceConfiguration config = entry.getValue();
if (config != null) {
for (PersistenceItemConfiguration itemConfig : config.getConfigs()) {
if (hasStrategy(config, itemConfig, PersistenceStrategy.Globals.RESTORE)) {
if (appliesToItem(itemConfig, item)) {
PersistenceService service = persistenceServices.get(serviceName);
if (service instanceof QueryablePersistenceService) {
QueryablePersistenceService queryService = (QueryablePersistenceService) service;
FilterCriteria filter = new FilterCriteria().setItemName(item.getName())
.setPageSize(1);
Iterable<HistoricItem> result = safeCaller
.create(queryService, QueryablePersistenceService.class).onTimeout(() -> {
logger.warn("Querying persistence service '{}' takes more than {}ms.",
queryService.getId(), SafeCaller.DEFAULT_TIMEOUT);
}).onException(e -> {
logger.error(
"Exception occurred while querying persistence service '{}': {}",
queryService.getId(), e.getMessage(), e);
}).build().query(filter);
if (result != null) {
Iterator<HistoricItem> it = result.iterator();
if (it.hasNext()) {
HistoricItem historicItem = it.next();
GenericItem genericItem = (GenericItem) item;
genericItem.removeStateChangeListener(this);
genericItem.setState(historicItem.getState());
genericItem.addStateChangeListener(this);
if (logger.isDebugEnabled()) {
logger.debug("Restored item state from '{}' for item '{}' -> '{}'",
DateTimeFormatter.ISO_ZONED_DATE_TIME
.format(historicItem.getTimestamp()),
item.getName(), historicItem.getState());
}
return;
}
}
} else if (service != null) {
logger.warn(
"Failed to restore item states as persistence service '{}' cannot be queried.",
serviceName);
}
}
}
}
}
}
}
}
private void removeItemStateChangeListeners() {
for (Item item : itemRegistry.getAll()) {
if (item instanceof GenericItem) {
((GenericItem) item).removeStateChangeListener(this);
}
}
}
/**
* Creates new {@link ScheduledCompletableFuture}s in the group <code>dbId</code> for the given collection of
* {@link PersistenceStrategy strategies}.
*
* @param dbId the database id used by the persistence service
* @param strategies a collection of strategies
*/
private void createTimers(final String dbId, List<PersistenceStrategy> strategies) {
for (PersistenceStrategy strategy : strategies) {
if (strategy instanceof PersistenceCronStrategy) {
PersistenceCronStrategy cronStrategy = (PersistenceCronStrategy) strategy;
String cronExpression = cronStrategy.getCronExpression();
final PersistItemsJob job = new PersistItemsJob(this, dbId, cronStrategy.getName());
ScheduledCompletableFuture<?> schedule = scheduler.schedule(job, cronExpression);
if (persistenceJobs.containsKey(dbId)) {
persistenceJobs.get(dbId).add(schedule);
} else {
final Set<ScheduledCompletableFuture<?>> jobs = new HashSet<>();
jobs.add(schedule);
persistenceJobs.put(dbId, jobs);
}
logger.debug("Scheduled strategy {} with cron expression {}", cronStrategy.getName(), cronExpression);
}
}
}
/**
* Deletes all {@link ScheduledCompletableFuture}s of the group <code>dbId</code>.
*
* @param dbId the database id used by the persistence service
*/
private void removeTimers(String dbId) {
if (!persistenceJobs.containsKey(dbId)) {
return;
}
for (final ScheduledCompletableFuture<?> job : persistenceJobs.get(dbId)) {
job.cancel(true);
logger.debug("Removed scheduled cron job for persistence service '{}'", dbId);
}
persistenceJobs.remove(dbId);
}
private void removeTimers() {
Set<String> dbIds = new HashSet<>(persistenceJobs.keySet());
for (String dbId : dbIds) {
removeTimers(dbId);
}
}
@Override
public void addConfig(final String dbId, final PersistenceServiceConfiguration config) {
synchronized (persistenceServiceConfigs) {
if (started && persistenceServiceConfigs.containsKey(dbId)) {
stopEventHandling(dbId);
}
persistenceServiceConfigs.put(dbId, config);
if (started && persistenceServices.containsKey(dbId)) {
startEventHandling(dbId);
}
}
}
@Override
public void removeConfig(final String dbId) {
synchronized (persistenceServiceConfigs) {
stopEventHandling(dbId);
PersistenceService persistenceService = persistenceServices.get(dbId);
if (persistenceService != null) {
persistenceServiceConfigs.put(dbId, getDefaultConfig(persistenceService));
startEventHandling(dbId);
} else {
persistenceServiceConfigs.remove(dbId);
}
}
}
private void startEventHandling(final String serviceName) {
synchronized (persistenceServiceConfigs) {
final PersistenceServiceConfiguration config = persistenceServiceConfigs.get(serviceName);
if (config != null) {
for (PersistenceItemConfiguration itemConfig : config.getConfigs()) {
if (hasStrategy(config, itemConfig, PersistenceStrategy.Globals.RESTORE)) {
for (Item item : getAllItems(itemConfig)) {
initialize(item);
}
}
}
createTimers(serviceName, config.getStrategies());
}
}
}
private void stopEventHandling(String dbId) {
synchronized (persistenceServiceConfigs) {
removeTimers(dbId);
}
}
private @Nullable PersistenceServiceConfiguration getDefaultConfig(PersistenceService persistenceService) {
List<PersistenceStrategy> strategies = persistenceService.getDefaultStrategies();
List<PersistenceItemConfiguration> configs = List
.of(new PersistenceItemConfiguration(List.of(new PersistenceAllConfig()), null, strategies, null));
return new PersistenceServiceConfiguration(configs, strategies, strategies);
}
@Override
public void allItemsChanged(Collection<String> oldItemNames) {
for (Item item : itemRegistry.getItems()) {
added(item);
}
}
@Override
public void added(Item item) {
initialize(item);
if (item instanceof GenericItem) {
GenericItem genericItem = (GenericItem) item;
genericItem.addStateChangeListener(this);
}
}
@Override
public void removed(Item item) {
if (item instanceof GenericItem) {
GenericItem genericItem = (GenericItem) item;
genericItem.removeStateChangeListener(this);
}
}
@Override
public void updated(Item oldItem, Item item) {
removed(oldItem);
added(item);
}
@Override
public void stateChanged(Item item, State oldState, State newState) {
handleStateEvent(item, true);
}
@Override
public void stateUpdated(Item item, State state) {
handleStateEvent(item, false);
}
@Override
public void onReadyMarkerAdded(ReadyMarker readyMarker) {
ExecutorService scheduler = Executors.newSingleThreadExecutor(new NamedThreadFactory("persistenceManager"));
scheduler.submit(() -> {
allItemsChanged(Collections.emptySet());
for (String dbId : persistenceServices.keySet()) {
startEventHandling(dbId);
}
started = true;
readyService.markReady(marker);
itemRegistry.addRegistryChangeListener(this);
});
scheduler.shutdown();
}
@Override
public void onReadyMarkerRemoved(ReadyMarker readyMarker) {
readyService.unmarkReady(marker);
}
}