Inheritance: IRegionManager
示例#1
0
        public void T00_ResumeEmpty()
        {
            T00_LogInit();
            IRegionManager rmgr = new RegionExposedFiles(InitMode.NEW_REGION, "c:\\BENDtst\\1");
            TestReceiver receiver = new TestReceiver();
            LogWriter lr = LogWriter.LogWriter_Resume(rmgr, receiver);

            Assert.AreEqual(receiver.cmds.Count, 0, "there should be no log records");
        }
        public void T02_Region_References()
        {
            RegionExposedFiles rm = new RegionExposedFiles(InitMode.NEW_REGION,
                    "C:\\BENDtst\\T01_Region_References");

            byte[] data = { 1, 3, 4, 5, 6, 7, 8, 9, 10 };
            bool delegate_called = false;

            {
                // put some data in the region
                IRegion region1 = rm.writeFreshRegionAddr(0, BLOCK_SIZE);
                {
                    Stream output = region1.getNewAccessStream();
                    output.Write(data, 0, data.Length);
                    output.Dispose();
                }
                region1 = null;
            }
            System.GC.Collect();

            {
                IRegion region2 = rm.readRegionAddrNonExcl(0);
                Stream rd1 = region2.getNewAccessStream();
                rm.notifyRegionSafeToFree(0, delegate(long addr) {
                    System.Console.WriteLine("** region safe to free delegate called");
                    delegate_called = true;
                });
                rd1 = null;
                region2 = null;
            }
            rm = null;

            for (int x = 0; x < 1000; x++) {
                Thread.Sleep(5);
                System.GC.Collect();
                if (delegate_called) { break; }
            }
            Assert.AreEqual(true, delegate_called, "region was never safe to free");
        }
示例#3
0
        public void T00_LogInit()
        {
            int system_reserved_space;

            IRegionManager rmgr = new RegionExposedFiles(InitMode.NEW_REGION, "c:\\BENDtst\\1");  // TODO, create random directory
            {

                LogWriter lr = LogWriter.LogWriter_NewRegion(rmgr, null, out system_reserved_space);
                lr.Dispose();
            }

            // open the rootblock stream...
            Stream rootblockstream = rmgr.readRegionAddr(0).getNewAccessStream();

            // read rootblock header
            rootblockstream.Seek(0, SeekOrigin.Begin);
            RootBlockHeader rootblockdata = Bend.Util.readStruct<RootBlockHeader>(rootblockstream);

            // check rootblock data
            {
                Assert.AreEqual(rootblockdata.magic, RootBlockHeader.ROOTMAGIC, "root block magic");
                Assert.AreEqual(rootblockdata.num_logsegments, LogWriter.DEFAULT_LOG_SEGMENTS);
                // TODO: check checksum
            }

            // read / check each log segment
            for (int i = 0; i < rootblockdata.num_logsegments; i++) {
                RootBlockLogSegment seg = Bend.Util.readStruct<RootBlockLogSegment>(rootblockstream);

                Stream logstream = rmgr.readRegionAddr(seg.logsegment_start).getNewAccessStream();
                logstream.Seek(0, SeekOrigin.Begin);

                // check that each log segment contains a valid closed log record
                logstream.Close();
            }

            rootblockstream.Close();
        }
        public void T05_Region_Concurrency()
        {
            RegionExposedFiles rm = new RegionExposedFiles(InitMode.NEW_REGION,
                    "C:\\BENDtst\\T05_Region_Concurrency");
            byte[] data = { 1, 3, 4, 5, 6, 7, 8, 9, 10 };

            {
                // put some data in the region
                IRegion region1 = rm.writeFreshRegionAddr(0, BLOCK_SIZE);
                {
                    Stream output = region1.getNewAccessStream();
                    output.Write(data, 0, data.Length);
                    output.Close();
                }
            }

            {
                IRegion region1 = rm.readRegionAddrNonExcl(0);
                Stream rd1 = region1.getNewAccessStream();

                Stream rd2 = region1.getNewAccessStream();

                // Assert.AreNotEqual(rd1, rd2, "streams should be separate");

                for (int i = 0; i < data.Length; i++) {
                    Assert.AreEqual(0, rd2.Position, "stream rd2 position should be indpendent");

                    Assert.AreEqual(i, rd1.Position, "stream rd1 position");
                    Assert.AreEqual(data[i], rd1.ReadByte(), "stream rd1 data correcness");
                }

            }
        }
示例#5
0
        public void T01_BigNumbers()
        {
            // 2147487744
            long number = 2147487744;
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            String number_s = enc.GetString(Lsd.numberToLsd(number, 13));
            Assert.AreEqual("0002147487744",number_s);

            // 4294971392
            number = 4294971392;
            var bytes = Lsd.numberToLsd(number, 13);
            number_s = enc.GetString(bytes);
            Assert.AreEqual("0004294971392", number_s);
            Assert.AreEqual(number, Lsd.lsdToNumber(bytes));

            // not really a bignumbers test, but this is failing at runtime, why!?!?
            var REF = new RegionExposedFiles(@"c:\foo");
            var filepath = REF.makeFilepath(number);
            Assert.AreEqual(@"c:\foo\addr0004294971392.rgm", filepath);
        }
示例#6
0
        public void T00_ResumeWithRecords()
        {
            IRegionManager rmgr = new RegionExposedFiles(InitMode.NEW_REGION, "c:\\BENDtst\\2");

            byte[] cmddata = { 0x81, 0x82, 0x83 };
            const int NUM_COMMANDS = 3;

            // make a new empty log
            {
                int system_reserved_space;
                LogWriter lr = LogWriter.LogWriter_NewRegion(rmgr, new DummyLogReceiver(), out system_reserved_space);
                lr.log_handler.setDebugLogSegments();

                Assert.AreEqual(1, lr.log_handler.activeLogSegments, "one active log segment");

                // add NUM_COMMANDS records to the log
                long logWaitNumber;
                for (int i=0;i<NUM_COMMANDS;i++) {
                    lr.addCommand(LogCommands.UPDATE, cmddata, out logWaitNumber);
                    lr.flushPendingCommands();

                    printLogStatus(lr, String.Format("addCommand iteration {0}", i));
                }
                lr.Dispose();
            }
            // reinit and resume from the log
            {
                TestReceiver receiver = new TestReceiver();
                LogWriter lr = LogWriter.LogWriter_Resume(rmgr, receiver);

                Assert.AreEqual(NUM_COMMANDS, lr.log_handler.activeLogSegments, "should be NUM_COMMANDS log segments");
                Assert.AreEqual(NUM_COMMANDS, receiver.cmds.Count, "number of log records incorrect");
                Assert.AreEqual(LogCommands.UPDATE, receiver.cmds[0].cmd, "cmdbyte should match");
                Assert.AreEqual(cmddata, receiver.cmds[0].cmddata, "cmddata should match");
                lr.Dispose();
            }
            // assert the log had the records
        }
示例#7
0
        public void T02_LogCheckpointResumeOrder()
        {
            IRegionManager rmgr = new RegionExposedFiles(InitMode.NEW_REGION, "c:\\BENDtst\\2");

            byte[] cmddata = { 0x00 };
            int timestamp = 5;
            long logWaitNumber;

            {
                // make a new empty log
                TestReceiver receiver = new TestReceiver();
                int system_reserved_space;
                LogWriter lr = LogWriter.LogWriter_NewRegion(rmgr, receiver, out system_reserved_space);
                lr.log_handler.setDebugLogSegments(); // force one command per log segment

                // find out how many empty segments there are...

                // (1) add commands to fill up the log segments..
                {
                    int emptySegments = lr.log_handler.emptyLogSegments;
                    for (int i = 0; i < emptySegments; i++) {
                        cmddata[0] = (byte)timestamp;
                        timestamp++;
                        lr.addCommand(LogCommands.UPDATE, cmddata, out logWaitNumber);
                        lr.flushPendingCommandsThrough(logWaitNumber);
                        printLogStatus(lr, String.Format("filling empty segments {0}", i));
                    }
                    Assert.AreEqual(1, lr.log_handler.emptyLogSegments, "should be no empty log segments 1");

                    printLogStatus(lr, "log almost full");
                }

                // (2) checkpoint the log
                {
                    lr.checkpointStart();

                    lr.checkpointDrop(out logWaitNumber);
                    lr.flushPendingCommandsThrough(logWaitNumber);

                    Assert.LessOrEqual(1, lr.log_handler.activeLogSegments, "should have only <= 2 active log segments");
                    printLogStatus(lr, "log checkpoint complete");
                }

                // (3) add commands to fill newly free log segments
                {
                    int emptySegments = lr.log_handler.emptyLogSegments;
                    for (int i = 0; i < emptySegments; i++) {
                        cmddata[0] = (byte)timestamp;
                        timestamp++;
                        lr.addCommand(LogCommands.UPDATE, cmddata, out logWaitNumber);
                        lr.flushPendingCommandsThrough(logWaitNumber);
                        printLogStatus(lr, String.Format("filling empty segments {0}", i));
                    }
                    Assert.LessOrEqual(lr.log_handler.emptyLogSegments, 1, "should be no empty log segments 2");
                    printLogStatus(lr, "log almost full 2");
                }

                // (4) shutdown
                lr.Dispose();
            }

            // (5) now resume, make sure we resume in order
            {
                TestReceiver receiver = new TestReceiver();
                LogWriter lr = LogWriter.LogWriter_Resume(rmgr, receiver);

                int cur = 0;
                foreach (var cmd in receiver.cmds) {
                    if (cmd.cmd == LogCommands.UPDATE) {
                        Console.WriteLine("Resume Record : {0} {1}", cmd.cmd.ToString(), cmd.cmddata[0]);
                        Assert.Greater(cmd.cmddata[0], cur, "order should be increasing");

                        cur = cmd.cmddata[0];
                    } else {
                        Console.WriteLine("empty command : {0}", cmd.cmd.ToString());
                    }
                }
            }
        }
示例#8
0
        public void T01_OverflowLog()
        {
            IRegionManager rmgr = new RegionExposedFiles(InitMode.NEW_REGION, "c:\\BENDtst\\2");

            byte[] cmddata = { 0x00 };

            {
                // make a new empty log
                TestReceiver receiver = new TestReceiver();
                int system_reserved_space;
                LogWriter lr = LogWriter.LogWriter_NewRegion(rmgr, receiver, out system_reserved_space);
                lr.log_handler.setDebugLogSegments(); // force one command per log segment

                // find out how many empty segments there are...
                int emptySegments = lr.log_handler.emptyLogSegments;

                // add a command to fill up the log segments..
                long logWaitNumber = 0;
                for (int i = 0; i <= emptySegments; i++) {
                    lr.addCommand(LogCommands.UPDATE, cmddata, out logWaitNumber);
                    lr.flushPendingCommands();
                }
                Assert.AreEqual(0, lr.log_handler.emptyLogSegments, "should be no empty log segments");

                // now add another command.. which should overflow the log segments and send us the log-extend request

                // make sure a write attempt blocks (or fails)

                // make sure a log-extend still succeeds.

                Assert.Fail("INTENTIONAL FAIL - test not fully implemented");

                lr.Dispose();
            }
        }
示例#9
0
        public void T01_LogCheckpoint()
        {
            IRegionManager rmgr = new RegionExposedFiles(InitMode.NEW_REGION, "c:\\BENDtst\\2");

            byte[] cmddata = { 0x81, 0x82, 0x83 };
            long logWaitNumber;

            {
                // make a new empty log
                TestReceiver receiver = new TestReceiver();
                int system_reserved_space;
                LogWriter lr = LogWriter.LogWriter_NewRegion(rmgr, receiver, out system_reserved_space);
                lr.log_handler.setDebugLogSegments(); // force one command per log segment

                // find out how many empty segments there are...
                int emptySegments = lr.log_handler.emptyLogSegments;

                // add a command to fill up the log segments..

                for (int i = 0; i < emptySegments; i++) {
                    lr.addCommand(LogCommands.UPDATE, cmddata, out logWaitNumber);
                    lr.flushPendingCommandsThrough(logWaitNumber);
                    printLogStatus(lr, String.Format("filling empty segments {0}",i));
                }
                Assert.AreEqual(1, lr.log_handler.emptyLogSegments, "should be no empty log segments");

                printLogStatus(lr, "log almost full");

                // now checkpoint the log
                lr.checkpointStart();

                lr.checkpointDrop(out logWaitNumber);
                lr.flushPendingCommandsThrough(logWaitNumber);

                Assert.LessOrEqual(2, lr.log_handler.activeLogSegments, "should have only <= 2 active log segments");
                printLogStatus(lr, "log checkpoint complete");

                lr.Dispose();
            }
        }