This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
/
CpEntityManagerFactory.java
754 lines (564 loc) · 26.8 KB
/
CpEntityManagerFactory.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
* Copyright 2014 The Apache Software Foundation.
*
* 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 org.apache.usergrid.corepersistence;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.usergrid.corepersistence.index.ReIndexRequestBuilder;
import org.apache.usergrid.persistence.*;
import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.apache.commons.lang.StringUtils;
import org.apache.usergrid.corepersistence.asyncevents.AsyncEventService;
import org.apache.usergrid.corepersistence.index.ReIndexService;
import org.apache.usergrid.corepersistence.pipeline.builder.PipelineBuilderFactory;
import org.apache.usergrid.corepersistence.util.CpNamingUtils;
import org.apache.usergrid.exception.ConflictException;
import org.apache.usergrid.persistence.cassandra.CassandraService;
import org.apache.usergrid.persistence.cassandra.CounterUtils;
import org.apache.usergrid.persistence.cassandra.Setup;
import org.apache.usergrid.persistence.collection.EntityCollectionManager;
import org.apache.usergrid.persistence.collection.serialization.impl.migration.EntityIdScope;
import org.apache.usergrid.persistence.core.metrics.MetricsFactory;
import org.apache.usergrid.persistence.core.migration.data.MigrationDataProvider;
import org.apache.usergrid.persistence.core.scope.ApplicationScope;
import org.apache.usergrid.persistence.core.scope.ApplicationScopeImpl;
import org.apache.usergrid.persistence.core.util.Health;
import org.apache.usergrid.persistence.entities.Application;
import org.apache.usergrid.persistence.exceptions.ApplicationAlreadyExistsException;
import org.apache.usergrid.persistence.exceptions.DuplicateUniquePropertyExistsException;
import org.apache.usergrid.persistence.graph.Edge;
import org.apache.usergrid.persistence.graph.GraphManager;
import org.apache.usergrid.persistence.graph.GraphManagerFactory;
import org.apache.usergrid.persistence.graph.SearchByEdgeType;
import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
import org.apache.usergrid.persistence.index.ApplicationEntityIndex;
import org.apache.usergrid.persistence.index.EntityIndex;
import org.apache.usergrid.persistence.index.EntityIndexFactory;
import org.apache.usergrid.persistence.index.IndexRefreshCommand;
import org.apache.usergrid.persistence.model.entity.Id;
import org.apache.usergrid.persistence.model.entity.SimpleId;
import org.apache.usergrid.persistence.model.util.UUIDGenerator;
import org.apache.usergrid.utils.UUIDUtils;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import rx.Observable;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
import static org.apache.usergrid.persistence.Schema.PROPERTY_APPLICATION_ID;
import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
import static org.apache.usergrid.persistence.Schema.TYPE_APPLICATION;
/**
* Implement good-old Usergrid EntityManagerFactory with the new-fangled Core Persistence API.
* This is where we keep track of applications and system properties.
*/
public class CpEntityManagerFactory implements EntityManagerFactory, ApplicationContextAware {
private static final Logger logger = LoggerFactory.getLogger( CpEntityManagerFactory.class );
private final EntityIndexFactory entityIndexFactory;
private final EntityManagerFig entityManagerFig;
private ApplicationContext applicationContext;
private Setup setup = null;
/** Have we already initialized the index for the management app? */
private AtomicBoolean indexInitialized = new AtomicBoolean( );
// cache of already instantiated entity managers
private LoadingCache<UUID, EntityManager> entityManagers
= CacheBuilder.newBuilder().maximumSize(100).build(new CacheLoader<UUID, EntityManager>() {
public EntityManager load(UUID appId) { // no checked exception
return _getEntityManager(appId);
}
});
private final ApplicationIdCache applicationIdCache;
private ManagerCache managerCache;
private CassandraService cassandraService;
private CounterUtils counterUtils;
private Injector injector;
private final ReIndexService reIndexService;
private final EntityIndex entityIndex;
private final MetricsFactory metricsFactory;
private final AsyncEventService indexService;
private final PipelineBuilderFactory pipelineBuilderFactory;
private final GraphManagerFactory graphManagerFactory;
public CpEntityManagerFactory( final CassandraService cassandraService, final CounterUtils counterUtils,
final Injector injector ) {
this.cassandraService = cassandraService;
this.counterUtils = counterUtils;
this.injector = injector;
this.reIndexService = injector.getInstance(ReIndexService.class);
this.entityManagerFig = injector.getInstance(EntityManagerFig.class);
this.entityIndex = injector.getInstance(EntityIndex.class);
this.entityIndexFactory = injector.getInstance(EntityIndexFactory.class);
this.managerCache = injector.getInstance( ManagerCache.class );
this.metricsFactory = injector.getInstance( MetricsFactory.class );
this.indexService = injector.getInstance( AsyncEventService.class );
this.pipelineBuilderFactory = injector.getInstance( PipelineBuilderFactory.class );
this.graphManagerFactory = injector.getInstance( GraphManagerFactory.class );
this.applicationIdCache = injector.getInstance(ApplicationIdCacheFactory.class).getInstance(
getManagementEntityManager() );
}
public CounterUtils getCounterUtils() {
return counterUtils;
}
public CassandraService getCassandraService() {
return cassandraService;
}
private void init() {
EntityManager em = getEntityManager(getManagementAppId());
try {
if ( em.getApplication() == null ) {
logger.info("Creating management application");
Map mgmtAppProps = new HashMap<String, Object>();
mgmtAppProps.put(PROPERTY_NAME, "systemapp");
em.create( getManagementAppId(), TYPE_APPLICATION, mgmtAppProps);
em.getApplication();
}
// entityIndex.refreshAsync();
} catch (Exception ex) {
throw new RuntimeException("Fatal error creating management application", ex);
}
}
private Observable<EntityIdScope> getAllEntitiesObservable(){
return injector.getInstance( Key.get(new TypeLiteral< MigrationDataProvider<EntityIdScope>>(){})).getData();
}
@Override
public EntityManager getEntityManager(UUID applicationId) {
try {
return entityManagers.get( applicationId );
}
catch ( Exception ex ) {
logger.error("Error getting oldAppInfo manager", ex);
}
return _getEntityManager(applicationId);
}
private EntityManager _getEntityManager( UUID applicationId ) {
EntityManager em = new CpEntityManager(cassandraService, counterUtils, indexService, managerCache,
metricsFactory, entityManagerFig, pipelineBuilderFactory, graphManagerFactory, applicationId );
return em;
}
@Override
public Entity createApplicationV2(String organizationName, String name) throws Exception {
return createApplicationV2(organizationName, name, null);
}
@Override
public Entity createApplicationV2(
String orgName, String name, Map<String, Object> properties) throws Exception {
String appName = buildAppName( orgName, name );
final Optional<UUID> appId = applicationIdCache.getApplicationId( appName );
if ( appId.isPresent()) {
throw new ApplicationAlreadyExistsException( name );
}
UUID applicationId = UUIDGenerator.newTimeUUID();
logger.debug( "New application orgName {} orgAppName {} id {} ",
new Object[] { orgName, name, applicationId.toString() } );
return initializeApplicationV2(orgName, applicationId, appName, properties);
}
private String buildAppName( String organizationName, String name ) {
return StringUtils.lowerCase(name.contains("/") ? name : organizationName + "/" + name);
}
/**
* @return UUID of newly created Entity of type application_info
*/
@Override
public Entity initializeApplicationV2( String organizationName, final UUID applicationId, String name,
Map<String, Object> properties ) throws Exception {
// Ensure our management system exists before creating our application
init();
EntityManager managementEm = getEntityManager( getManagementAppId() );
final String appName = buildAppName( organizationName, name );
// check for pre-existing application
if ( lookupApplication( appName ).isPresent()) {
throw new ApplicationAlreadyExistsException( appName );
}
getSetup().setupApplicationKeyspace( applicationId, appName );
if ( properties == null ) {
properties = new TreeMap<>( CASE_INSENSITIVE_ORDER );
}
properties.put( PROPERTY_NAME, appName );
EntityManager appEm = getEntityManager(applicationId);
appEm.create(applicationId, TYPE_APPLICATION, properties);
appEm.resetRoles();
// entityIndex.refreshAsync();//.toBlocking().last();
// create application info entity in the management app
Map<String, Object> appInfoMap = new HashMap<String, Object>() {{
put( PROPERTY_NAME, appName );
put( PROPERTY_APPLICATION_ID, applicationId );
}};
Entity appInfo;
try {
appInfo = managementEm.create(new SimpleId(applicationId,CpNamingUtils.APPLICATION_INFO), appInfoMap);
} catch (DuplicateUniquePropertyExistsException e) {
throw new ApplicationAlreadyExistsException(appName);
}
// evict app Id from cache
applicationIdCache.evictAppId(appName);
logger.info("Initialized application {}", appName);
return appInfo;
}
/**
* Delete Application.
*
* <p>The Application Entity is be moved to a Deleted_Applications collection and the
* Application index will be removed.
*
* <p>TODO: add scheduled task that can completely delete all deleted application data.</p>
*
* @param applicationId UUID of Application to be deleted.
*/
@Override
public void deleteApplication(UUID applicationId) throws Exception {
// find application_info for application to delete
String collectionFromName = CpNamingUtils.APPLICATION_INFO;
String collectionToName = CpNamingUtils.DELETED_APPLICATION_INFO;
migrateAppInfo(applicationId, collectionFromName, collectionToName).toBlocking()
.lastOrDefault(null);
}
//TODO: return status for restore
@Override
public Entity restoreApplication(UUID applicationId) throws Exception {
// get the deleted_application_info for the deleted app
return (Entity) migrateAppInfo(applicationId, CpNamingUtils.DELETED_APPLICATION_INFO, CpNamingUtils.APPLICATION_INFO)
.map(o -> {
final ReIndexRequestBuilder builder =
reIndexService.getBuilder().withApplicationId(applicationId);
return reIndexService.rebuildIndex(builder);
})
.map(status -> {
final EntityManager managementEm = getEntityManager(getManagementAppId());
try {
return managementEm.get(new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationId));
} catch (Exception e) {
logger.error("Failed to get entity", e);
throw new RuntimeException(e);
}
})
.toBlocking().lastOrDefault(null);
}
@Override
public Observable migrateAppInfo(UUID applicationUUID, String collectionFromName, String collectionToName) throws Exception {
final ApplicationScope managementAppScope = CpNamingUtils.getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
final EntityManager managementEm = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
final Id applicationId = new SimpleId(applicationUUID, collectionFromName);
final ApplicationScope applicationScope = new ApplicationScopeImpl(applicationId);
Entity oldAppEntity = managementEm.get(new SimpleEntityRef(collectionFromName, applicationUUID));
Observable copyConnections = Observable.empty();
if (oldAppEntity != null) {
// ensure that there is not already a deleted app with the same name
final EntityRef alias = managementEm.getAlias(collectionToName, oldAppEntity.getName());
if (alias != null) {
throw new ConflictException("Cannot delete app with same name as already deleted app");
}
// make a copy of the app to delete application_info entity
// and put it in a deleted_application_info collection
final Entity newAppEntity = managementEm.create(new SimpleId(applicationUUID,
collectionToName), oldAppEntity.getProperties());
// copy its connections too
final Set<String> connectionTypes = managementEm.getConnectionTypes(oldAppEntity);
copyConnections = Observable.from(connectionTypes).doOnNext(connType -> {
try {
final Results connResults =
managementEm.getTargetEntities(oldAppEntity, connType, null, Query.Level.ALL_PROPERTIES);
connResults.getEntities().forEach(entity -> {
try {
managementEm.createConnection(newAppEntity, connType, entity);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
final Id managementAppId = CpNamingUtils.getManagementApplicationId();
final ApplicationEntityIndex aei = entityIndexFactory.createApplicationEntityIndex(applicationScope);
final GraphManager managementGraphManager = managerCache.getGraphManager(managementAppScope);
final Edge createEdge = CpNamingUtils.createCollectionEdge(managementAppId, collectionToName, applicationId);
final Observable compactObservable = managementGraphManager.compactNode(applicationId);
final Observable deleteNodeGraph = managementGraphManager
.markNode(applicationId, CpNamingUtils.createGraphOperationTimestamp())
.flatMap(id -> compactObservable);
final Observable createNodeGraph = managementGraphManager.writeEdge(createEdge);
final Observable deleteAppFromIndex = aei.deleteApplication();
return Observable
.merge(copyConnections, createNodeGraph, deleteNodeGraph, deleteAppFromIndex)
.doOnCompleted(() -> {
try {
if (oldAppEntity != null) {
managementEm.delete(oldAppEntity);
applicationIdCache.evictAppId(oldAppEntity.getName());
}
entityIndex.refreshAsync().toBlocking().last();
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
@Override
public UUID importApplication(
String organization, UUID applicationId,
String name, Map<String, Object> properties) throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
public Optional<UUID> lookupApplication( String orgAppName ) throws Exception {
return applicationIdCache.getApplicationId(orgAppName);
}
@Override
public Map<String, UUID> getApplications() throws Exception {
return getApplications(false);
}
@Override
public Map<String, UUID> getDeletedApplications() throws Exception {
return getApplications( true );
}
public Map<String, UUID> getApplications(boolean deleted) throws Exception {
Map<String, UUID> appMap = new HashMap<>();
ApplicationScope appScope =
CpNamingUtils.getApplicationScope(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
GraphManager gm = managerCache.getGraphManager(appScope);
EntityManager managementEM = getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
Application managementApp = managementEM.getApplication();
if( managementApp == null ) {
throw new RuntimeException("Management App "
+ CpNamingUtils.MANAGEMENT_APPLICATION_ID + " should never be null");
}
Id managementId = new SimpleId( managementApp.getUuid(), managementApp.getType() );
final String edgeType;
if ( deleted ) {
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( CpNamingUtils.DELETED_APPLICATION_INFOS );
} else {
edgeType = CpNamingUtils.getEdgeTypeFromCollectionName( CpNamingUtils.APPLICATION_INFOS );
}
logger.debug("getApplications(): Loading edges of edgeType {} from {}:{}",
new Object[]{edgeType, managementId.getType(), managementId.getUuid()});
Observable<Edge> edges = gm.loadEdgesFromSource( new SimpleSearchByEdgeType(
managementId, edgeType, Long.MAX_VALUE,
SearchByEdgeType.Order.DESCENDING, Optional.<Edge>absent() ));
// TODO This is wrong, and will result in OOM if there are too many applications.
// This needs to stream properly with a buffer
edges.doOnNext(edge -> {
Id targetId = edge.getTargetNode();
logger.debug("getApplications(): Processing edge from {}:{} to {}:{}", new Object[]{
edge.getSourceNode().getType(), edge.getSourceNode().getUuid(),
edge.getTargetNode().getType(), edge.getTargetNode().getUuid()
});
org.apache.usergrid.persistence.model.entity.Entity appInfo =
managerCache.getEntityCollectionManager(appScope).load(targetId)
.toBlocking().lastOrDefault(null);
if (appInfo == null) {
logger.warn("Application {} has edge but not found in em", targetId);
return;
}
UUID applicationId = UUIDUtils.tryExtractUUID(
appInfo.getField(PROPERTY_APPLICATION_ID).getValue().toString());
appMap.put((String) appInfo.getField(PROPERTY_NAME).getValue(), applicationId);
}).toBlocking().lastOrDefault(null);
return appMap;
}
@Override
public void setup() throws Exception {
getSetup().init();
init();
}
@Override
public Map<String, String> getServiceProperties() {
Map<String, String> props = new HashMap<String,String>();
EntityManager em = getEntityManager(getManagementAppId());
Query q = Query.fromQL("select *");
Results results = null;
try {
results = em.searchCollection( em.getApplicationRef(), "propertymaps", q);
} catch (Exception ex) {
logger.error("Error getting system properties", ex);
}
if ( results == null || results.isEmpty() ) {
return props;
}
org.apache.usergrid.persistence.Entity e = results.getEntity();
for ( String key : e.getProperties().keySet() ) {
props.put( key, props.get(key).toString() );
}
return props;
}
@Override
public boolean updateServiceProperties(Map<String, String> properties) {
EntityManager em = getEntityManager(getManagementAppId());
Query q = Query.fromQL("select *");
Results results = null;
try {
results = em.searchCollection( em.getApplicationRef(), "propertymaps", q);
} catch (Exception ex) {
logger.error("Error getting system properties", ex);
return false;
}
org.apache.usergrid.persistence.Entity propsEntity = null;
if ( !results.isEmpty() ) {
propsEntity = results.getEntity();
} else {
propsEntity = EntityFactory.newEntity( UUIDUtils.newTimeUUID(), "propertymap");
}
// intentionally going only one-level deep into fields and treating all
// values as strings because that is all we need for service properties
for ( String key : properties.keySet() ) {
propsEntity.setProperty(key, properties.get(key).toString());
}
try {
em.update( propsEntity );
} catch (Exception ex) {
logger.error("Error updating service properties", ex);
return false;
}
return true;
}
@Override
public boolean setServiceProperty(final String name, final String value) {
return updateServiceProperties(new HashMap<String, String>() {{
put(name, value);
}});
}
@Override
public boolean deleteServiceProperty(String name) {
EntityManager em = getEntityManager(getManagementAppId());
Query q = Query.fromQL( "select *");
Results results = null;
try {
results = em.searchCollection( em.getApplicationRef(), "propertymaps", q);
} catch (Exception ex) {
logger.error("Error getting service property for delete of property: " + name, ex);
return false;
}
org.apache.usergrid.persistence.Entity propsEntity = null;
if ( !results.isEmpty() ) {
propsEntity = results.getEntity();
} else {
propsEntity = EntityFactory.newEntity( UUIDUtils.newTimeUUID(), "propertymap");
}
try {
((AbstractEntity)propsEntity).clearDataset( name );
em.update( propsEntity );
} catch (Exception ex) {
logger.error("Error deleting service property orgAppName: " + name, ex);
return false;
}
return true;
}
public ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException {
this.applicationContext = applicationContext;
// try {
// setup();
// } catch (Exception ex) {
// logger.error("Error setting up EMF", ex);
// }
}
@Override
public long performEntityCount() {
//TODO, this really needs to be a task that writes this data somewhere since this will get
//progressively slower as the system expands
return (Long) getAllEntitiesObservable().countLong().toBlocking().last();
}
@Override
public UUID getManagementAppId() {
return CpNamingUtils.MANAGEMENT_APPLICATION_ID;
}
@Override
public EntityManager getManagementEntityManager() {
return getEntityManager(CpNamingUtils.MANAGEMENT_APPLICATION_ID);
}
/**
* Gets the setup.
* @return Setup helper
*/
public Setup getSetup() {
if ( setup == null ) {
setup = new CpSetup( this, cassandraService, injector );
}
return setup;
}
/**
* TODO, these 3 methods are super janky. During refactoring we should clean this model up
*/
public IndexRefreshCommand.IndexRefreshCommandInfo refreshIndex() {
// refresh special indexes without calling EntityManager refresh because stack overflow
maybeCreateIndexes();
return entityIndex.refreshAsync().toBlocking().first();
}
private void maybeCreateIndexes() {
if ( indexInitialized.getAndSet( true ) ) {
return;
}
// entityIndex.initializeIndex();
}
private List<ApplicationEntityIndex> getManagementIndexes() {
return Arrays.asList(
managerCache.getEntityIndex( // management app
CpNamingUtils.getApplicationScope(getManagementAppId())));
}
@Override
public void flushEntityManagerCaches() {
managerCache.invalidate();
applicationIdCache.evictAll();
Map<UUID, EntityManager> entityManagersMap = entityManagers.asMap();
for ( UUID appUuid : entityManagersMap.keySet() ) {
EntityManager em = entityManagersMap.get(appUuid);
em.flushManagerCaches();
}
}
@Override
public void addIndex(final String indexSuffix,final int shards,final int replicas, final String writeConsistency){
entityIndex.addIndex( indexSuffix, shards, replicas, writeConsistency);
}
@Override
public Health getEntityStoreHealth() {
// could use any collection scope here, does not matter
EntityCollectionManager ecm = managerCache.getEntityCollectionManager(
new ApplicationScopeImpl( new SimpleId( CpNamingUtils.MANAGEMENT_APPLICATION_ID, "application" ) ) );
return ecm.getHealth();
}
@Override
public UUID createApplication(String organizationName, String name) throws Exception {
throw new UnsupportedOperationException("Not supported in v2");
}
@Override
public UUID createApplication(
String organizationName, String name, Map<String, Object> properties) throws Exception {
throw new UnsupportedOperationException("Not supported in v2");
}
@Override
public UUID initializeApplication(
String orgName, UUID appId, String appName, Map<String, Object> props) throws Exception {
throw new UnsupportedOperationException("Not supported in v2");
}
@Override
public Health getIndexHealth() {
return entityIndex.getIndexHealth();
}
}