Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
fix cp head entity null
Browse files Browse the repository at this point in the history
  • Loading branch information
Shawn Feldman committed Apr 6, 2015
1 parent f5d77b1 commit 552361b
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 43 deletions.
Expand Up @@ -348,11 +348,25 @@ public <A extends TypedEntity> A create( A entity ) throws Exception {
public Entity create( UUID importId, String entityType, Map<String, Object> properties ) throws Exception { public Entity create( UUID importId, String entityType, Map<String, Object> properties ) throws Exception {


UUID timestampUuid = importId != null ? importId : UUIDUtils.newTimeUUID(); UUID timestampUuid = importId != null ? importId : UUIDUtils.newTimeUUID();
Keyspace ko = cass.getApplicationKeyspace( applicationId );
Mutator<ByteBuffer> m = createMutator( ko, be );

Entity entity = batchCreate( m,entityType, null, properties, importId, timestampUuid );

//Adding graphite metrics
Timer.Context timeCassCreation = entCreateTimer.time();
m.execute();
timeCassCreation.stop();
return entity;
}

@Override
public Entity create( Id id, Map<String, Object> properties ) throws Exception {


Keyspace ko = cass.getApplicationKeyspace( applicationId ); Keyspace ko = cass.getApplicationKeyspace( applicationId );
Mutator<ByteBuffer> m = createMutator( ko, be ); Mutator<ByteBuffer> m = createMutator( ko, be );


Entity entity = batchCreate( m, entityType, null, properties, importId, timestampUuid ); Entity entity = batchCreate( m, id.getType(), null, properties, id.getUuid(), UUIDUtils.newTimeUUID() );


//Adding graphite metrics //Adding graphite metrics
Timer.Context timeCassCreation = entCreateTimer.time(); Timer.Context timeCassCreation = entCreateTimer.time();
Expand Down Expand Up @@ -727,9 +741,10 @@ public void updateApplication( Map<String, Object> properties ) throws Exception


@Override @Override
public RelationManager getRelationManager( EntityRef entityRef ) { public RelationManager getRelationManager( EntityRef entityRef ) {
Preconditions.checkNotNull( entityRef, "entityRef cannot be null" ); Preconditions.checkNotNull(entityRef, "entityRef cannot be null");
CpRelationManager rmi = new CpRelationManager(); CpRelationManager rmi = CpRelationManagerFactory.get(
rmi.init( this, emf, applicationId, entityRef, null, metricsFactory ); this, emf, applicationId, entityRef, null, metricsFactory
);
return rmi; return rmi;
} }


Expand Down
Expand Up @@ -244,7 +244,7 @@ public Entity initializeApplicationV2( String organizationName, final UUID appli
// Ensure our management system exists before creating our application // Ensure our management system exists before creating our application
init(); init();


EntityManager em = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID ); EntityManager managementEm = getEntityManager( CpNamingUtils.MANAGEMENT_APPLICATION_ID );


final String appName = buildAppName( organizationName, name ); final String appName = buildAppName( organizationName, name );


Expand Down Expand Up @@ -274,7 +274,7 @@ public Entity initializeApplicationV2( String organizationName, final UUID appli


Entity appInfo; Entity appInfo;
try { try {
appInfo = em.create(CpNamingUtils.APPLICATION_INFO, appInfoMap); appInfo = managementEm.create(applicationId,CpNamingUtils.APPLICATION_INFO, appInfoMap);
} catch (DuplicateUniquePropertyExistsException e) { } catch (DuplicateUniquePropertyExistsException e) {
throw new ApplicationAlreadyExistsException(appName); throw new ApplicationAlreadyExistsException(appName);
} }
Expand Down Expand Up @@ -312,11 +312,16 @@ public void deleteApplication(UUID applicationId) throws Exception {


String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS); String edgeType = CpNamingUtils.getEdgeTypeFromCollectionName(CpNamingUtils.APPLICATION_INFOS);


//TODO: this could be due to app info issue
Observable<Edge> appInfoEdges = managementGraphManager.loadEdgesFromSource(new SimpleSearchByEdgeType( Observable<Edge> appInfoEdges = managementGraphManager.loadEdgesFromSource(new SimpleSearchByEdgeType(
managementAppId, edgeType, Long.MAX_VALUE, managementAppId, edgeType, Long.MAX_VALUE,
SearchByEdgeType.Order.DESCENDING, null)); SearchByEdgeType.Order.DESCENDING, null));


appInfoEdges.flatMap(appInfoEdge -> { appInfoEdges
.filter(appInfoEdge -> {
return appInfoEdge.getTargetNode().getUuid().equals(applicationId);
})
.flatMap(appInfoEdge -> {
try { try {
Entity appInfoToDelete = managementEm.get(appInfoEdge.getTargetNode().getUuid()); Entity appInfoToDelete = managementEm.get(appInfoEdge.getTargetNode().getUuid());
final Id deletedAppId = new SimpleId(appInfoToDelete.getUuid(), appInfoToDelete.getType()); final Id deletedAppId = new SimpleId(appInfoToDelete.getUuid(), appInfoToDelete.getType());
Expand Down
Expand Up @@ -203,56 +203,46 @@ public CpRelationManager init(
IndexBucketLocator indexBucketLocator, IndexBucketLocator indexBucketLocator,
MetricsFactory metricsFactory) { MetricsFactory metricsFactory) {


Assert.notNull( em, "Entity manager cannot be null" ); Assert.notNull(em, "Entity manager cannot be null");
Assert.notNull( emf, "Entity manager factory cannot be null" ); Assert.notNull(emf, "Entity manager factory cannot be null");
Assert.notNull( applicationId, "Application Id cannot be null" ); Assert.notNull(applicationId, "Application Id cannot be null");
Assert.notNull( headEntity, "Head entity cannot be null" ); Assert.notNull(headEntity, "Head entity cannot be null");
Assert.notNull( headEntity.getUuid(), "Head entity uuid cannot be null" ); Assert.notNull(headEntity.getUuid(), "Head entity uuid cannot be null");

// TODO: this assert should not be failing // TODO: this assert should not be failing
//Assert.notNull( indexBucketLocator, "indexBucketLocator cannot be null" ); //Assert.notNull( indexBucketLocator, "indexBucketLocator cannot be null" );

this.em = em; this.em = em;
this.emf = emf; this.emf = emf;
this.applicationId = applicationId; this.applicationId = applicationId;
this.headEntity = headEntity; this.headEntity = headEntity;
this.managerCache = emf.getManagerCache(); this.managerCache = emf.getManagerCache();
this.applicationScope = CpNamingUtils.getApplicationScope( applicationId ); this.applicationScope = CpNamingUtils.getApplicationScope(applicationId);

this.cass = em.getCass(); // TODO: eliminate need for this via Core Persistence this.cass = em.getCass(); // TODO: eliminate need for this via Core Persistence
this.indexBucketLocator = indexBucketLocator; // TODO: this also this.indexBucketLocator = indexBucketLocator; // TODO: this also
this.metricsFactory = metricsFactory; this.metricsFactory = metricsFactory;
this.updateCollectionTimer = metricsFactory this.updateCollectionTimer = metricsFactory
.getTimer( CpRelationManager.class, "relation.manager.es.update.collection" ); .getTimer(CpRelationManager.class, "relation.manager.es.update.collection");
this.createConnectionTimer = metricsFactory this.createConnectionTimer = metricsFactory
.getTimer( CpRelationManager.class, "relation.manager.es.create.connection.timer" ); .getTimer(CpRelationManager.class, "relation.manager.es.create.connection.timer");
this.cassConnectionDelete = metricsFactory this.cassConnectionDelete = metricsFactory
.getTimer( CpRelationManager.class, "relation.manager.cassandra.delete.connection.batch.timer" ); .getTimer(CpRelationManager.class, "relation.manager.cassandra.delete.connection.batch.timer");
this.esDeleteConnectionTimer = metricsFactory.getTimer( CpRelationManager.class, this.esDeleteConnectionTimer = metricsFactory.getTimer(CpRelationManager.class,
"relation.manager.es.delete.connection.batch.timer" ); "relation.manager.es.delete.connection.batch.timer");



if (logger.isDebugEnabled()) {
if ( logger.isDebugEnabled() ) { logger.debug("Loading head entity {}:{} from app {}",
logger.debug( "Loading head entity {}:{} from app {}", new Object[]{
new Object[] {
headEntity.getType(), headEntity.getType(),
headEntity.getUuid(), headEntity.getUuid(),
applicationScope applicationScope
} ); });
} }


Id entityId = new SimpleId( headEntity.getUuid(), headEntity.getType() ); Id entityId = new SimpleId(headEntity.getUuid(), headEntity.getType());


// if(headEntity instanceof Entity){ this.cpHeadEntity = ((CpEntityManager) em).load(entityId);
// cpHeadEntity = entityToCpEntity( (Entity)headEntity, headEntity.getUuid() );
// }else {
this.cpHeadEntity =
( ( CpEntityManager ) em ).load( entityId );
// }


// commented out because it is possible that CP entity has not been created yet // commented out because it is possible that CP entity has not been created yet
Assert.notNull( cpHeadEntity, "cpHeadEntity cannot be null" ); Assert.notNull(cpHeadEntity, "cpHeadEntity cannot be null for app id " + applicationScope.getApplication().getUuid());



return this; return this;
} }
Expand Down
@@ -0,0 +1,46 @@
/*
*
* * Licensed to the Apache Software Foundation (ASF) under one or more
* * contributor license agreements. The ASF licenses this file to You
* * 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. For additional information regarding
* * copyright in this work, please see the NOTICE file in the top level
* * directory of this distribution.
*
*/
package org.apache.usergrid.corepersistence;

import com.google.inject.Inject;
import org.apache.usergrid.persistence.EntityManager;
import org.apache.usergrid.persistence.EntityRef;
import org.apache.usergrid.persistence.IndexBucketLocator;
import org.apache.usergrid.persistence.core.metrics.MetricsFactory;

import java.util.UUID;

/**
* Factory to return and init relation manager instances
*/
public class CpRelationManagerFactory {


public static CpRelationManager get( EntityManager em,
CpEntityManagerFactory emf,
UUID applicationId,
EntityRef headEntity,
IndexBucketLocator indexBucketLocator,
MetricsFactory metricsFactory){
CpRelationManager relationManager = new CpRelationManager();
relationManager.init(em,emf,applicationId,headEntity,indexBucketLocator,metricsFactory);
return relationManager;
}
}
Expand Up @@ -35,6 +35,7 @@
import org.apache.usergrid.persistence.index.query.CounterResolution; import org.apache.usergrid.persistence.index.query.CounterResolution;
import org.apache.usergrid.persistence.index.query.Identifier; import org.apache.usergrid.persistence.index.query.Identifier;
import org.apache.usergrid.persistence.index.query.Query.Level; import org.apache.usergrid.persistence.index.query.Query.Level;
import org.apache.usergrid.persistence.model.entity.Id;




/** /**
Expand Down Expand Up @@ -93,6 +94,15 @@ public <A extends Entity> A create( String entityType, Class<A> entityClass, Map
public Entity create( UUID importId, String entityType, Map<String, Object> properties ) public Entity create( UUID importId, String entityType, Map<String, Object> properties )
throws Exception; throws Exception;


/**
* Creates an entity of the specified type attached to the specified application.
* @param id
* @param properties
* @return
* @throws Exception
*/
public Entity create(Id id, Map<String, Object> properties )
throws Exception;
public void createApplicationCollection( String entityType ) throws Exception; public void createApplicationCollection( String entityType ) throws Exception;


public EntityRef getAlias( String aliasType, String alias ) throws Exception; public EntityRef getAlias( String aliasType, String alias ) throws Exception;
Expand Down
Expand Up @@ -36,7 +36,9 @@


import javax.annotation.Resource; import javax.annotation.Resource;


import org.apache.usergrid.exception.NotImplementedException;
import org.apache.usergrid.persistence.index.EntityIndex; import org.apache.usergrid.persistence.index.EntityIndex;
import org.apache.usergrid.persistence.model.entity.Id;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -706,6 +708,11 @@ public Entity create( UUID importId, String entityType, Map<String, Object> prop
return create( entityType, null, properties, importId ); return create( entityType, null, properties, importId );
} }


@Override
public Entity create(Id id, Map<String, Object> properties) throws Exception {
throw new NotImplementedException();
}



@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
@Override @Override
Expand Down
Expand Up @@ -32,6 +32,9 @@
import org.apache.usergrid.exception.ConflictException; import org.apache.usergrid.exception.ConflictException;
import org.apache.usergrid.management.exceptions.*; import org.apache.usergrid.management.exceptions.*;
import org.apache.usergrid.persistence.*; import org.apache.usergrid.persistence.*;
import org.apache.usergrid.persistence.graph.Edge;
import org.apache.usergrid.persistence.graph.SearchByEdgeType;
import org.apache.usergrid.persistence.graph.impl.SimpleSearchByEdgeType;
import org.apache.usergrid.persistence.index.query.Query; import org.apache.usergrid.persistence.index.query.Query;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -144,6 +147,8 @@
import static org.apache.usergrid.persistence.entities.Activity.PROPERTY_TITLE; import static org.apache.usergrid.persistence.entities.Activity.PROPERTY_TITLE;
import static org.apache.usergrid.persistence.entities.Activity.PROPERTY_VERB; import static org.apache.usergrid.persistence.entities.Activity.PROPERTY_VERB;
import org.apache.usergrid.persistence.index.query.Query.Level; import org.apache.usergrid.persistence.index.query.Query.Level;
import rx.Observable;

import static org.apache.usergrid.security.AuthPrincipalType.ADMIN_USER; import static org.apache.usergrid.security.AuthPrincipalType.ADMIN_USER;
import static org.apache.usergrid.security.AuthPrincipalType.APPLICATION; import static org.apache.usergrid.security.AuthPrincipalType.APPLICATION;
import static org.apache.usergrid.security.AuthPrincipalType.APPLICATION_USER; import static org.apache.usergrid.security.AuthPrincipalType.APPLICATION_USER;
Expand Down Expand Up @@ -1611,8 +1616,6 @@ public ApplicationInfo createApplication( UUID organizationId, String applicatio
properties = new HashMap<>(); properties = new HashMap<>();
} }


EntityManager em = emf.getEntityManager( smf.getManagementAppId() );

OrganizationInfo organizationInfo = getOrganizationByUuid( organizationId ); OrganizationInfo organizationInfo = getOrganizationByUuid( organizationId );
Entity appInfo = emf.createApplicationV2( Entity appInfo = emf.createApplicationV2(
organizationInfo.getName(), applicationName, properties); organizationInfo.getName(), applicationName, properties);
Expand Down Expand Up @@ -1711,7 +1714,7 @@ public OrganizationInfo getOrganizationForApplication( UUID applicationInfoId )
return null; return null;
} }


EntityManager em = emf.getEntityManager( smf.getManagementAppId() ); final EntityManager em = emf.getEntityManager( smf.getManagementAppId() );


Results r = em.getConnectingEntities( Results r = em.getConnectingEntities(
new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId), new SimpleEntityRef(CpNamingUtils.APPLICATION_INFO, applicationInfoId),
Expand Down Expand Up @@ -1786,8 +1789,7 @@ public BiMap<UUID, String> getApplicationsForOrganizations( Set<UUID> organizati
@Override @Override
public UUID addApplicationToOrganization(UUID organizationId, Entity appInfo) throws Exception { public UUID addApplicationToOrganization(UUID organizationId, Entity appInfo) throws Exception {


UUID applicationId = UUIDUtils.tryExtractUUID( UUID applicationId = appInfo.getUuid();
appInfo.getProperty(PROPERTY_APPLICATION_ID).toString());


if ( ( organizationId == null ) || ( applicationId == null ) ) { if ( ( organizationId == null ) || ( applicationId == null ) ) {
return null; return null;
Expand Down
Expand Up @@ -79,16 +79,16 @@ public void testCreateOrganization() throws Exception {
organization.getOrganization().getUuid() ); organization.getOrganization().getUuid() );
assertEquals( "wrong number of users", 1, users.size() ); assertEquals( "wrong number of users", 1, users.size() );


UUID applicationId = setup.getMgmtSvc().createApplication( organization.getOrganization().getUuid(), "ed-application" ).getId(); ApplicationInfo applicationInfo = setup.getMgmtSvc().createApplication(organization.getOrganization().getUuid(), "ed-application");
assertNotNull( applicationId ); assertNotNull( applicationInfo.getId() );




setup.getEntityIndex().refresh(); setup.getEntityIndex().refresh();


Map<UUID, String> applications = setup.getMgmtSvc().getApplicationsForOrganization( organization.getOrganization().getUuid() ); Map<UUID, String> applications = setup.getMgmtSvc().getApplicationsForOrganization( organization.getOrganization().getUuid() );
assertEquals( "wrong number of applications", 1, applications.size() ); assertEquals( "wrong number of applications", 1, applications.size() );


OrganizationInfo organization2 = setup.getMgmtSvc().getOrganizationForApplication( applicationId ); OrganizationInfo organization2 = setup.getMgmtSvc().getOrganizationForApplication( applicationInfo.getId() );
assertNotNull( organization2 ); assertNotNull( organization2 );
assertEquals( "wrong organization name", organization.getOrganization().getName(), organization2.getName() ); assertEquals( "wrong organization name", organization.getOrganization().getName(), organization2.getName() );


Expand Down

0 comments on commit 552361b

Please sign in to comment.