public virtual TimelinePutResponse Put(TimelineEntities data) { lock (this) { TimelinePutResponse response = new TimelinePutResponse(); foreach (TimelineEntity entity in data.GetEntities()) { EntityIdentifier entityId = new EntityIdentifier(entity.GetEntityId(), entity.GetEntityType ()); // store entity info in memory TimelineEntity existingEntity = entities[entityId]; if (existingEntity == null) { existingEntity = new TimelineEntity(); existingEntity.SetEntityId(entity.GetEntityId()); existingEntity.SetEntityType(entity.GetEntityType()); existingEntity.SetStartTime(entity.GetStartTime()); if (entity.GetDomainId() == null || entity.GetDomainId().Length == 0) { TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityId(entityId.GetId()); error.SetEntityType(entityId.GetType()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.NoDomain); response.AddError(error); continue; } existingEntity.SetDomainId(entity.GetDomainId()); entities[entityId] = existingEntity; entityInsertTimes[entityId] = Runtime.CurrentTimeMillis(); } if (entity.GetEvents() != null) { if (existingEntity.GetEvents() == null) { existingEntity.SetEvents(entity.GetEvents()); } else { existingEntity.AddEvents(entity.GetEvents()); } existingEntity.GetEvents().Sort(); } // check startTime if (existingEntity.GetStartTime() == null) { if (existingEntity.GetEvents() == null || existingEntity.GetEvents().IsEmpty()) { TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityId(entityId.GetId()); error.SetEntityType(entityId.GetType()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.NoStartTime); response.AddError(error); Sharpen.Collections.Remove(entities, entityId); Sharpen.Collections.Remove(entityInsertTimes, entityId); continue; } else { long min = long.MaxValue; foreach (TimelineEvent e in entity.GetEvents()) { if (min > e.GetTimestamp()) { min = e.GetTimestamp(); } } existingEntity.SetStartTime(min); } } if (entity.GetPrimaryFilters() != null) { if (existingEntity.GetPrimaryFilters() == null) { existingEntity.SetPrimaryFilters(new Dictionary <string, ICollection <object> >()); } foreach (KeyValuePair <string, ICollection <object> > pf in entity.GetPrimaryFilters ()) { foreach (object pfo in pf.Value) { existingEntity.AddPrimaryFilter(pf.Key, MaybeConvert(pfo)); } } } if (entity.GetOtherInfo() != null) { if (existingEntity.GetOtherInfo() == null) { existingEntity.SetOtherInfo(new Dictionary <string, object>()); } foreach (KeyValuePair <string, object> info in entity.GetOtherInfo()) { existingEntity.AddOtherInfo(info.Key, MaybeConvert(info.Value)); } } // relate it to other entities if (entity.GetRelatedEntities() == null) { continue; } foreach (KeyValuePair <string, ICollection <string> > partRelatedEntities in entity. GetRelatedEntities()) { if (partRelatedEntities == null) { continue; } foreach (string idStr in partRelatedEntities.Value) { EntityIdentifier relatedEntityId = new EntityIdentifier(idStr, partRelatedEntities .Key); TimelineEntity relatedEntity = entities[relatedEntityId]; if (relatedEntity != null) { if (relatedEntity.GetDomainId().Equals(existingEntity.GetDomainId())) { relatedEntity.AddRelatedEntity(existingEntity.GetEntityType(), existingEntity.GetEntityId ()); } else { // in this case the entity will be put, but the relation will be // ignored TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityType(existingEntity.GetEntityType()); error.SetEntityId(existingEntity.GetEntityId()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.ForbiddenRelation); response.AddError(error); } } else { relatedEntity = new TimelineEntity(); relatedEntity.SetEntityId(relatedEntityId.GetId()); relatedEntity.SetEntityType(relatedEntityId.GetType()); relatedEntity.SetStartTime(existingEntity.GetStartTime()); relatedEntity.AddRelatedEntity(existingEntity.GetEntityType(), existingEntity.GetEntityId ()); relatedEntity.SetDomainId(existingEntity.GetDomainId()); entities[relatedEntityId] = relatedEntity; entityInsertTimes[relatedEntityId] = Runtime.CurrentTimeMillis(); } } } } return(response); } }
/// <summary> /// Store the timeline entities into the store and set the owner of them to the /// given user. /// </summary> /// <exception cref="Org.Apache.Hadoop.Yarn.Exceptions.YarnException"/> /// <exception cref="System.IO.IOException"/> public virtual TimelinePutResponse PostEntities(TimelineEntities entities, UserGroupInformation callerUGI) { if (entities == null) { return(new TimelinePutResponse()); } IList <EntityIdentifier> entityIDs = new AList <EntityIdentifier>(); TimelineEntities entitiesToPut = new TimelineEntities(); IList <TimelinePutResponse.TimelinePutError> errors = new AList <TimelinePutResponse.TimelinePutError >(); foreach (TimelineEntity entity in entities.GetEntities()) { EntityIdentifier entityID = new EntityIdentifier(entity.GetEntityId(), entity.GetEntityType ()); // if the domain id is not specified, the entity will be put into // the default domain if (entity.GetDomainId() == null || entity.GetDomainId().Length == 0) { entity.SetDomainId(DefaultDomainId); } // check if there is existing entity TimelineEntity existingEntity = null; try { existingEntity = store.GetEntity(entityID.GetId(), entityID.GetType(), EnumSet.Of (TimelineReader.Field.PrimaryFilters)); if (existingEntity != null) { AddDefaultDomainIdIfAbsent(existingEntity); if (!existingEntity.GetDomainId().Equals(entity.GetDomainId())) { throw new YarnException("The domain of the timeline entity " + entityID + " is not allowed to be changed." ); } } if (!timelineACLsManager.CheckAccess(callerUGI, ApplicationAccessType.ModifyApp, entity)) { throw new YarnException(callerUGI + " is not allowed to put the timeline entity " + entityID + " into the domain " + entity.GetDomainId() + "."); } } catch (Exception e) { // Skip the entity which already exists and was put by others Log.Error("Skip the timeline entity: " + entityID, e); TimelinePutResponse.TimelinePutError error = new TimelinePutResponse.TimelinePutError (); error.SetEntityId(entityID.GetId()); error.SetEntityType(entityID.GetType()); error.SetErrorCode(TimelinePutResponse.TimelinePutError.AccessDenied); errors.AddItem(error); continue; } entityIDs.AddItem(entityID); entitiesToPut.AddEntity(entity); if (Log.IsDebugEnabled()) { Log.Debug("Storing the entity " + entityID + ", JSON-style content: " + TimelineUtils .DumpTimelineRecordtoJSON(entity)); } } if (Log.IsDebugEnabled()) { Log.Debug("Storing entities: " + StringHelper.CsvJoiner.Join(entityIDs)); } TimelinePutResponse response = store.Put(entitiesToPut); // add the errors of timeline system filter key conflict response.AddErrors(errors); return(response); }