How to pass value between the Trigger methods (eg:AllowPut and Onput)

49 views
Skip to first unread message

Tim Zhang

unread,
Aug 12, 2014, 1:55:34 AM8/12/14
to rav...@googlegroups.com
As the following code shows,
In the AllowPut methods I set the IsReplicated = true and In the OnPut methods I want used this value.
Does this usage is correct?


public class ReplicationPurposedAttachmentPutTrigger : AbstractAttachmentPutTrigger
    {
        private bool IsReplicated{get;set;}

        public override VetoResult AllowPut(string key, Stream data, RavenJObject metadata)
        {
          
            IsReplicated = true;
            return VetoResult.Allowed;
        }

        public override void OnPut(string key, Stream data, RavenJObject metadata)
        {
            if(IsReplicated)
            {
                do something.
            }
        }

     
    }

Oren Eini (Ayende Rahien)

unread,
Aug 12, 2014, 5:21:40 AM8/12/14
to ravendb
You would need to create a ThreadLocal value and use that.



Oren Eini

CEO


Mobile: + 972-52-548-6969

Office:  + 972-4-622-7811

Fax:      + 972-153-4622-7811





--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Zhang

unread,
Sep 20, 2014, 9:13:07 AM9/20/14
to rav...@googlegroups.com

So does that means that one trigger instance will be shared by multiple thread or just one thread?

在 2014年8月12日星期二UTC+8下午5时21分40秒,Oren Eini写道:

Oren Eini (Ayende Rahien)

unread,
Sep 21, 2014, 5:19:31 AM9/21/14
to ravendb
Yes, trigger instances are shared per db.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 

Tim Zhang

unread,
Sep 22, 2014, 5:11:19 AM9/22/14
to rav...@googlegroups.com
If I declare private fields as ThreadLocal  do I need to Dispose the ThreadLocal field?
if I need dispose when and how I do that?


在 2014年9月21日星期日UTC+8下午5时19分31秒,Oren Eini写道:

Oren Eini (Ayende Rahien)

unread,
Sep 22, 2014, 5:13:33 AM9/22/14
to ravendb
Yes, if your trigger implements IDisposable, it will be called.

Tim Zhang

unread,
Oct 29, 2014, 3:36:43 AM10/29/14
to rav...@googlegroups.com

  When I batch delete  documents and all the documents are deleted at the same thread.
  So that means the private variable  "NsbrUnsentDelDocSnapshotKey"  value will be overwrite by the last document that apply the "AfterDelete".
  So  the value of "NsbrUnsentDelDocSnapshotKey"  in the "AfterCommit" will always the same which is not correct.

  so how can pass value between trigger methods.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

 public class SendCommandForDocumentDeleteTrigger : AbstractDeleteTrigger,IDisposable
    {
        private static Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private ThreadLocal<RavenJObject> DeletingDocMetaData = new ThreadLocal<RavenJObject>();
        private ThreadLocal<string> NsbrUnsentDelDocSnapshotKey = new ThreadLocal<string>();
        public override void OnDelete(string key, Raven.Abstractions.Data.TransactionInformation transactionInformation)
        {
            var doc = Database.Get(key,transactionInformation);
            if (doc != null)
            {
                DeletingDocMetaData.Value = doc.Metadata;
            }
            base.OnDelete(key, transactionInformation);
        }
        public override void AfterDelete(string key, TransactionInformation transactionInformation)
        {
            if (!MessageDefinition.IsSystemDocument(key) && DeletingDocMetaData!=null)
            {
                if (TriggerHelper.UseRobustWayToCatchUnsentMessage(Database))
                {
                    NsbrUnsentDelDocSnapshotKey.Value = RavenConstant.NsbrUnsentDocumentSnapshotKeyPrefix + Guid.NewGuid();
                    StoreFailedSendingMessage(NsbrUnsentDelDocSnapshotKey.Value, key, DeletingDocMetaData.Value, transactionInformation);
                }
            }
            base.AfterDelete(key, transactionInformation);
        }
        public override void AfterCommit(string key)
        {
            if (MessageDefinition.IsSystemDocument(key))
            {
                return;
            }
            if (DeletingDocMetaData==null)
            {
                return;
            }

            try
            {
                var replicationSourceInfoJson = ReplicationHelper.CreateReplicationSourceJson(GlobalBus.HostName,
                    Database.Name, "");
                var message = new DeleteDocument
                {
                    Key = key,
                    ReplicationSourceInfoJson = replicationSourceInfoJson,
                    MetadataJsonString = DeletingDocMetaData.ToString()
                };
                GlobalBus.Bus.Send(message);
                if (TriggerHelper.UseRobustWayToCatchUnsentMessage(Database))
                {
                    Database.Delete(NsbrUnsentDelDocSnapshotKey.Value, null, null);
                }
            }
            catch (Exception e)
            {
                if (!TriggerHelper.UseRobustWayToCatchUnsentMessage(Database))
                {
                    var snapshotkey= RavenConstant.NsbrUnsentDocumentSnapshotKeyPrefix + Guid.NewGuid();
                    try
                    {
                        StoreFailedSendingMessage(snapshotkey, key, DeletingDocMetaData.Value);
                    }
                    catch (Exception ex)
                    {
                        logger.Fatal("Store DeleteDocument Error:({0}-snapshotkey={1}) {2}", key,snapshotkey, ex);
                    }
                }
                logger.FatalException("SendCommandForDocumentDeleteTrigger Error", e);
            }

        }

        private void StoreFailedSendingMessage(string snapshortkey ,string key,RavenJObject deleteMetadata,
            TransactionInformation transactionInformation = null)
        {
                var ravenJObject =
                    RavenJObject.FromObject(new NsbrUnsentDocumentSnapshot
                    {
                        Key = key,
                        ActionType = RavenConstant.NsbrUnsentMessageActionTypeDelete,
                        SourceDb = Database.Name,
                        SourceHost = GlobalBus.HostName,
                        Metadata = deleteMetadata.ToString()
                    }
                    );
                var metadata = new RavenJObject()
                {
                    {Constants.RavenEntityName,RavenJToken.FromObject(RavenConstant.NsbrUnsentDocumentSnapshotKeyPrefix.TrimEnd('/'))}
                };
                Database.Put(snapshortkey, null, ravenJObject, metadata, transactionInformation);
        }

        public void Dispose()
        {
            if (DeletingDocMetaData != null)
            {
                DeletingDocMetaData.Dispose();
            }
            if (NsbrUnsentDelDocSnapshotKey != null)
            {
                NsbrUnsentDelDocSnapshotKey.Dispose();
            }
        }
    }

Oren Eini (Ayende Rahien)

unread,
Oct 29, 2014, 7:28:51 AM10/29/14
to ravendb
Use a thread local dictionary, in that case.

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
Reply all
Reply to author
Forward
0 new messages