public void Dulicate_message_is_detected()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(Message));

            var builder = new CqrsEngineBuilder(streamer);

            var cfg = new MemoryStorageConfig();

            var sender = cfg.CreateSimpleSender(streamer, "in");

            builder.Handle(cfg.CreateInbox("in"), envelope => Console.WriteLine("Got message"));

            var env = new EnvelopeBuilder("fixed ID").Build();

            using (var token = new CancellationTokenSource())
                using (var build = builder.Build())
                    using (TestObserver.When <EnvelopeDuplicateDiscarded>(discarded => token.Cancel()))
                    {
                        sender.SendBatch(new object[] { new Message() }, IdGeneration.HashContent);
                        sender.SendBatch(new object[] { new Message() }, IdGeneration.HashContent);
                        build.Start(token.Token);

                        if (Debugger.IsAttached)
                        {
                            token.Token.WaitHandle.WaitOne();
                        }
                        else
                        {
                            token.Token.WaitHandle.WaitOne(10000);
                        }

                        Assert.IsTrue(token.IsCancellationRequested);
                    }
        }
Esempio n. 2
0
        public void SimpleWriteRoundtrip()
        {
            var strategy = new TestStrategy();
            var setup    = Compose(strategy);

            setup.AddOrUpdateEntity(1, "test");
            setup.AddOrUpdateSingleton(() => 1, i => 1);

            AssertContents(setup);

            var memStore = new MemoryStorageConfig();
            var mem      = memStore.CreateNuclear(strategy);


            mem.CopyFrom(setup, strategy.GetEntityBucket <string>(), strategy.GetEntityBucket <int>());

            AssertContents(mem);

            setup.Container.Reset(strategy.GetEntityBucket <string>());
            setup.Container.Reset(strategy.GetEntityBucket <int>());

            setup.CopyFrom(mem, strategy.GetEntityBucket <string>(), strategy.GetEntityBucket <int>());

            AssertContents(setup);
        }
        public void Dulicate_message_is_detected()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(Message));

            var builder = new CqrsEngineBuilder(streamer);

            var cfg = new MemoryStorageConfig();

            var sender = cfg.CreateSimpleSender(streamer, "in");
            builder.Handle(cfg.CreateInbox("in"), envelope => Console.WriteLine("Got message"));

            var env = new EnvelopeBuilder("fixed ID").Build();

            using (var token = new CancellationTokenSource())
            using (var build = builder.Build())
            using (TestObserver.When<EnvelopeDuplicateDiscarded>(discarded => token.Cancel()))
            {
                sender.SendBatch(new object[]{new Message()}, IdGeneration.HashContent);
                sender.SendBatch(new object[] { new Message()}, IdGeneration.HashContent);
                build.Start(token.Token);

                if (Debugger.IsAttached)
                {
                    token.Token.WaitHandle.WaitOne();
                }
                else
                {
                    token.Token.WaitHandle.WaitOne(10000);
                }

                Assert.IsTrue(token.IsCancellationRequested);
            }
        }
 protected override Setup ComposeComponents(IEnvelopeStreamer streamer)
 {
     var config = new MemoryStorageConfig();
     return new Setup
     {
         Inbox = config.CreateInbox("in"),
         Sender = config.CreateSimpleSender(streamer, "in"),
         Storage = config.CreateNuclear(new TestStrategy())
     };
 }
 protected override Setup ConfigureComponents(IEnvelopeStreamer config)
 {
     var acc = new MemoryStorageConfig();
     return new Setup
         {
             Store = acc.CreateNuclear(new TestStrategy()),
             Inbox = acc.CreateInbox("queue"),
             Sender = acc.CreateSimpleSender(config, "queue")
         };
 }
        protected override Setup ConfigureComponents(IEnvelopeStreamer config)
        {
            var acc = new MemoryStorageConfig();

            return(new Setup
            {
                Store = acc.CreateNuclear(new TestStrategy()),
                Inbox = acc.CreateInbox("queue"),
                Sender = acc.CreateSimpleSender(config, "queue")
            });
        }
        protected override Setup ConfigureComponents(IEnvelopeStreamer streamer)
        {
            var _config = new MemoryStorageConfig();

            return(new Setup
            {
                Store = _config.CreateNuclear(new TestStrategy()),
                Inbox = _config.CreateInbox("dev"),
                Sender = _config.CreateSimpleSender(streamer, "dev")
            });
        }
        protected override Setup ComposeComponents(IEnvelopeStreamer streamer)
        {
            var config = new MemoryStorageConfig();

            return(new Setup
            {
                Inbox = config.CreateInbox("in"),
                Sender = config.CreateSimpleSender(streamer, "in"),
                Storage = config.CreateNuclear(new TestStrategy())
            });
        }
        protected override Setup ConfigureComponents(IEnvelopeStreamer streamer)
        {
            var _config = new MemoryStorageConfig();
            return new Setup
            {
                Store = _config.CreateNuclear(new TestStrategy()),
                Inbox = _config.CreateInbox("dev"),
                Sender = _config.CreateSimpleSender(streamer, "dev")

            };
        }
        public void SimpleWriteRoundtrip()
        {
            var strategy = new DefaultAtomicStorageStrategyBuilder().Build();
            var setup = Compose(strategy);

            setup.AddOrUpdateEntity(1, "test");
            setup.AddOrUpdateSingleton(() => 1, i => 1);

            AssertContents(setup);

            var mem = new MemoryStorageConfig().CreateNuclear(strategy);
            mem.CopyFrom(setup);

            AssertContents(mem);

            setup.Reset();

            setup.CopyFrom(mem);

            AssertContents(setup);
        }
        public void SimpleWriteRoundtrip()
        {
            var strategy = new TestStrategy();
            var setup = Compose(strategy);

            setup.AddOrUpdateEntity(1, "test");
            setup.AddOrUpdateSingleton(() => 1, i => 1);

            AssertContents(setup);

            var memStore = new MemoryStorageConfig();
            var mem = memStore.CreateNuclear(strategy);

            mem.CopyFrom(setup, strategy.GetEntityBucket<string>(), strategy.GetEntityBucket<int>());

            AssertContents(mem);

            setup.Container.Reset(strategy.GetEntityBucket<string>());
            setup.Container.Reset(strategy.GetEntityBucket<int>());

            setup.CopyFrom(mem, strategy.GetEntityBucket<string>(), strategy.GetEntityBucket<int>());

            AssertContents(setup);
        }
Esempio n. 12
0
        public static void Rebuild(CancellationToken token, IDocumentStore targetContainer, MessageStore stream, Func <IDocumentStore, IEnumerable <object> > projectors)
        {
            var strategy = targetContainer.Strategy;
            var memory   = new MemoryStorageConfig();

            var memoryContainer = memory.CreateNuclear(strategy).Container;
            var tracked         = new ProjectionInspectingStore(memoryContainer);

            var projections = new List <object>();

            projections.AddRange(projectors(tracked));

            if (tracked.Projections.Count != projections.Count())
            {
                throw new InvalidOperationException("Count mismatch");
            }
            tracked.ValidateSanity();

            var storage         = new NuclearStorage(targetContainer);
            var persistedHashes = new Dictionary <string, string>();
            var name            = "domain";

            storage.GetEntity <ProjectionHash>(name).IfValue(v => persistedHashes = v.BucketHashes);

            var activeMemoryProjections = projections.Select((projection, i) =>
            {
                var proj       = tracked.Projections[i];
                var bucketName = proj.StoreBucket;
                var viewType   = proj.EntityType;

                var projectionHash =
                    "Global change on 2012-08-24\r\n" +
                    GetClassHash(projection.GetType()) +
                    "\r\n " + GetClassHash(viewType) + "\r\n" + GetClassHash(strategy.GetType());

                bool needsRebuild = !persistedHashes.ContainsKey(bucketName) || persistedHashes[bucketName] != projectionHash;
                return(new
                {
                    bucketName,
                    projection,
                    hash = projectionHash,
                    needsRebuild
                });
            }).ToArray();

            foreach (var memoryProjection in activeMemoryProjections)
            {
                if (memoryProjection.needsRebuild)
                {
                    SystemObserver.Notify("[warn] {0} needs rebuild", memoryProjection.bucketName);
                }
                else
                {
                    SystemObserver.Notify("[good] {0} is up-to-date", memoryProjection.bucketName);
                }
            }


            var needRebuild = activeMemoryProjections.Where(x => x.needsRebuild).ToArray();

            if (needRebuild.Length == 0)
            {
                return;
            }


            var watch = Stopwatch.StartNew();

            var wire = new RedirectToDynamicEvent();

            needRebuild.ForEach(x => wire.WireToWhen(x.projection));


            var handlersWatch = Stopwatch.StartNew();



            ObserveWhileCan(stream.EnumerateAllItems(0, int.MaxValue), wire, token);

            if (token.IsCancellationRequested)
            {
                SystemObserver.Notify("[warn] Aborting projections before anything was changed");
                return;
            }

            var timeTotal      = watch.Elapsed.TotalSeconds;
            var handlerTicks   = handlersWatch.ElapsedTicks;
            var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1);

            SystemObserver.Notify("Total Elapsed: {0}sec ({1}sec in handlers)", Math.Round(timeTotal, 0), timeInHandlers);


            // update projections that need rebuild
            foreach (var b in needRebuild)
            {
                // server might shut down the process soon anyway, but we'll be
                // in partially consistent mode (not all projections updated)
                // so at least we blow up between projection buckets
                token.ThrowIfCancellationRequested();

                var bucketName = b.bucketName;
                var bucketHash = b.hash;

                // wipe contents
                targetContainer.Reset(bucketName);
                // write new versions
                var contents = memoryContainer.EnumerateContents(bucketName);
                targetContainer.WriteContents(bucketName, contents);

                // update hash
                storage.UpdateEntityEnforcingNew <ProjectionHash>(name, x =>
                {
                    x.BucketHashes[bucketName] = bucketHash;
                });

                SystemObserver.Notify("[good] Updated View bucket {0}.{1}", name, bucketName);
            }

            // Clean up obsolete views
            var allBuckets      = new HashSet <string>(activeMemoryProjections.Select(p => p.bucketName));
            var obsoleteBuckets = persistedHashes.Where(s => !allBuckets.Contains(s.Key)).ToArray();

            foreach (var hash in obsoleteBuckets)
            {
                // quit at this stage without any bad side effects
                if (token.IsCancellationRequested)
                {
                    return;
                }

                var bucketName = hash.Key;
                SystemObserver.Notify("[warn] {0} is obsolete", bucketName);
                targetContainer.Reset(bucketName);

                storage.UpdateEntityEnforcingNew <ProjectionHash>(name, x => x.BucketHashes.Remove(bucketName));

                SystemObserver.Notify("[good] Cleaned up obsolete view bucket {0}.{1}", name, bucketName);
            }
        }
 public MemoryQueueWriterFactory(MemoryStorageConfig storageConfig, string endpoint = "memory")
 {
     _storageConfig = storageConfig;
     _endpoint = endpoint;
 }
Esempio n. 14
0
 public MemoryQueueWriterFactory(MemoryStorageConfig storageConfig, string endpoint = "memory")
 {
     _storageConfig = storageConfig;
     _endpoint      = endpoint;
 }
        public static void Rebuild(IDocumentStore targetContainer, ITapeStream stream)
        {
            var strategy = targetContainer.Strategy;
            var memory   = new MemoryStorageConfig();

            var memoryContainer = memory.CreateNuclear(strategy).Container;
            var tracked         = new ProjectionInspectingContainer(memoryContainer);

            var projections = new List <object>();

            projections.AddRange(DomainBoundedContext.Projections(tracked));
            projections.AddRange(ClientBoundedContext.Projections(tracked));
            //projections.AddRange(ApiOpsBoundedContext.Projections(tracked));

            if (tracked.Buckets.Count != projections.Count())
            {
                throw new InvalidOperationException("Count mismatch");
            }

            var storage = new NuclearStorage(targetContainer);
            var hashes  = storage.GetSingletonOrNew <ProjectionHash>().Entries;

            var memoryProjections = projections.Select((projection, i) =>
            {
                var bucketName = tracked.Buckets[i];
                var viewType   = tracked.Views[i];

                var projectionHash = GetClassHash(projection.GetType()) + "\r\n" + GetClassHash(viewType);

                bool needsRebuild = !hashes.ContainsKey(bucketName) || hashes[bucketName] != projectionHash;
                return(new
                {
                    bucketName,
                    projection,
                    hash = projectionHash,
                    needsRebuild
                });
            }).ToArray();

            foreach (var memoryProjection in memoryProjections)
            {
                if (memoryProjection.needsRebuild)
                {
                    SystemObserver.Notify("[warn] {0} needs rebuild", memoryProjection.bucketName);
                }
                else
                {
                    SystemObserver.Notify("[good] {0} is up-to-date", memoryProjection.bucketName);
                }
            }


            var needRebuild = memoryProjections.Where(x => x.needsRebuild).ToArray();

            if (needRebuild.Length == 0)
            {
                return;
            }



            var watch = Stopwatch.StartNew();

            var wire = new RedirectToDynamicEvent();

            needRebuild.ForEach(x => wire.WireToWhen(x.projection));


            var handlersWatch = Stopwatch.StartNew();

            Observe(stream, wire);
            var timeTotal      = watch.Elapsed.TotalSeconds;
            var handlerTicks   = handlersWatch.ElapsedTicks;
            var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1);

            Console.WriteLine("Total Elapsed: {0}sec ({1}sec in handlers)", Math.Round(timeTotal, 0), timeInHandlers);


            // delete projections that were rebuilt
            var bucketNames = needRebuild.Select(x => x.bucketName).ToArray();

            foreach (var name in bucketNames)
            {
                targetContainer.Reset(name);

                var contents = memoryContainer.EnumerateContents(name);
                targetContainer.WriteContents(name, contents);
            }

            var allBuckets = new HashSet <string>(memoryProjections.Select(p => p.bucketName));
            var obsolete   = hashes.Keys.Where(s => !allBuckets.Contains(s)).ToArray();

            foreach (var name in obsolete)
            {
                SystemObserver.Notify("[warn] {0} is obsolete", name);
                targetContainer.Reset(name);
            }
            storage.UpdateSingletonEnforcingNew <ProjectionHash>(x =>
            {
                x.Entries.Clear();

                foreach (var prj in memoryProjections)
                {
                    x.Entries[prj.bucketName] = prj.hash;
                }
            });
        }