Skip to content

Instantly share code, notes, and snippets.

@tjayr
Last active August 29, 2015 13:57
Show Gist options
  • Save tjayr/9902681 to your computer and use it in GitHub Desktop.
Save tjayr/9902681 to your computer and use it in GitHub Desktop.
Topology sync algorithm - typically 53-58 seconds to write 950 MOs for initial sync of empty database and averages about 25 seconds to do a resync where no objects are created.
/** Current released version **/
public int invokeTopologySync(final ComponentEvent event, final Map<String, Object> headersSpec) {
// walk the tree and persist MOs into DPS
final Map<String, Object> payload = ((Map<String, Object>) event.getPayload());
final Set<String> fdnsTree = payload.keySet();
initModelServiceOperations(headersSpec);
final String mecfdn = (String) headersSpec.get("fdn");
try {
if (txManager.getTransaction() == null) {
txManager.begin();
}
} catch (NotSupportedException | SystemException e) {
log.error("Error occurred while attempting to start a transaction for topology sync {} ", e);
}
final DataBucket liveBucket = dpService.getDataBucket("Live", BucketProperties.SUPPRESS_MEDIATION, BucketProperties.SUPPRESS_CONSTRAINTS);
final Map<String, ModelInfo> mapping = modelServiceOperations.getNamespaceMapping();
for (final String fdn : fdnsTree) {
final String parentFdn = getParentFdn(fdn, mecfdn);
log.debug("Found parent of {} as {}", fdn, parentFdn);
final ManagedObject parent = liveBucket.findMoByFdn(parentFdn);
if (parent != null) {
final String moName = getManagedObjectName(fdn);
final String moType = getManagedObjectType(fdn);
final String fullFdn = parentFdn + "," + moType + "=" + moName;
final ManagedObject existingMo = liveBucket.findMoByFdn(fullFdn);
if (existingMo == null) {
log.debug("Creating managed object {} of type {} in dps", moName, moType);
final ModelInfo mi = mapping.get(moType);
if (mi != null) {
final ModelVersionInfo version = mi.getVersion();
final String versionString = version.toString();
log.debug("Creating MibRoot {} with version {}", versionString);
liveBucket.getMibRootBuilder().namespace(mi.getNamespace()).name(moName).type(moType).parent(parent).version(versionString)
.create();
} else {
liveBucket.getManagedObjectBuilder().name(moName).type(moType).parent(parent).create();
}
} else {
log.debug("Found Mo:[{}] that exist in DPS, ignoring it and continue", existingMo.getFdn());
}
} else {
log.error("INFO_FAILED_PARENT_NODE_DOES_NOT_EXIST");
try {
log.error("Rolling back topology transaction due to missing parent node");
txManager.rollback();
} catch (IllegalStateException | SecurityException | SystemException e) {
throw new IllegalStateException("Parent node doesn't exist, cannot create orphan child, fdn: " + fdn, e);
}
}
}
/** Branch : attempt to reduce dps calls **/
public int invokeTopologySync(final ComponentEvent event, final Map<String, Object> headersSpec) {
// walk the tree and persist MOs into DPS
final Map<String, Object> payload = ((Map<String, Object>) event.getPayload());
final Set<String> fdnsTree = payload.keySet();
initModelServiceOperations(headersSpec);
final String mecfdn = (String) headersSpec.get("fdn");
final DataBucket liveBucket = dpService.getDataBucket("Live", BucketProperties.SUPPRESS_MEDIATION, BucketProperties.SUPPRESS_CONSTRAINTS);
final Map<String, ModelInfo> mapping = modelServiceOperations.getNamespaceMapping();
ManagedObject parent = null;
String previousParentFdn = null;
for (final String fdn : fdnsTree) {
final String parentFdn = getParentFdn(fdn, mecfdn);
log.debug("Found parent of {} as {}", fdn, parentFdn);
if (previousParentFdn == null) {
previousParentFdn = parentFdn;
parent = liveBucket.findMoByFdn(parentFdn);
} else {
if (!previousParentFdn.equals(parentFdn)) {
parent = liveBucket.findMoByFdn(parentFdn);
}
}
if (parent != null) {
final String moName = getManagedObjectName(fdn);
final String moType = getManagedObjectType(fdn);
final String fullFdn = parentFdn + "," + moType + "=" + moName;
final ManagedObject existingMo = liveBucket.findMoByFdn(fullFdn);
if (existingMo == null) {
writeManagedObject(liveBucket, mapping, parent, moName, moType);
} else {
log.debug("Found Mo:[{}] that exist in DPS, ignoring it and continue", existingMo.getFdn());
}
} else {
log.debug("INFO_FAILED_PARENT_NODE_DOES_NOT_EXIST");
throw new IllegalStateException("Parent node doesn't exist, cannot create orphan child, fdn: " + fdn);
}
}
return fdnsTree.size();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment