Ejemplo n.º 1
0
        // GET: Ads/Edit/5
        public async Task <IActionResult> Edit(short?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            string accessToken = await HttpContext.GetTokenAsync("access_token");

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
            client.BaseAddress = new Uri(Configuration["URLAPI"] + "api/Ads/" + id);

            var ad = await client.GetStringAsync(client.BaseAddress);

            Ad ads = JsonConvert.DeserializeObject <Ad>(ad);

            if (ads == null)
            {
                return(NotFound());
            }

            AdInput adInput = new AdInput();

            adInput.Id          = ads.Id;
            adInput.Titre       = ads.Titre;
            adInput.Description = ads.Description;
            adInput.Link        = ads.Link;

            return(View(adInput));
        }
Ejemplo n.º 2
0
        private void IncrementAllValues(
            ref ClientSession <AdId, NumClicks, AdInput, Output, Empty, SimpleFunctions> fht,
            ref long sno)
        {
            AdInput inputArg = default;

            for (int key = 0; key < numOps; key++, sno++)
            {
                inputArg.adId.adId           = key;
                inputArg.numClicks.numClicks = 1;
                fht.RMW(ref inputArg.adId, ref inputArg, Empty.Default, sno);
            }
            fht.CompletePending(true);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(short id, [Bind("Id,Titre,Description,file,Link")] AdInput ad)
        {
            if (id != ad.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    string accessToken = await HttpContext.GetTokenAsync("access_token");

                    HttpClient client = new HttpClient();
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    client.BaseAddress = new Uri(Configuration["URLAPI"] + "api/Ads/" + ad.Id);

                    var           imageContent  = new StreamContent(ad.file.OpenReadStream());
                    StreamContent streamContent = new StreamContent(ad.file.OpenReadStream());
                    var           memoryStream  = new MemoryStream();
                    await streamContent.CopyToAsync(memoryStream);

                    var    bytes  = memoryStream.ToArray();
                    string base64 = Convert.ToBase64String(bytes);
                    double d      = base64.Length;

                    AdPost adPost = new AdPost();
                    adPost.Id                 = ad.Id;
                    adPost.Titre              = ad.Titre;
                    adPost.Description        = ad.Description;
                    adPost.file               = base64;
                    adPost.fileName           = ad.file.FileName;
                    adPost.name               = ad.file.Name;
                    adPost.ContentDisposition = ad.file.ContentDisposition;
                    adPost.ContentType        = ad.file.ContentType;
                    adPost.Link               = ad.Link;

                    string json = await Task.Run(() => JsonConvert.SerializeObject(adPost));

                    var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
                    await client.PutAsync(client.BaseAddress, httpContent);
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(NotFound());
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(ad));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult <Ad> > PostAd(AdPost adPost)
        {
            string folder = "Ads";

            AdInput ad = new AdInput();

            ad.Id          = adPost.Id;
            ad.Titre       = adPost.Titre;
            ad.Description = adPost.Description;
            ad.Link        = adPost.Link;

            byte[]    data   = Convert.FromBase64String(adPost.file);
            var       stream = new MemoryStream(data);
            IFormFile file   = new FormFile(stream, 0, data.Length, adPost.name, adPost.fileName)
            {
                Headers            = new HeaderDictionary(),
                ContentType        = adPost.ContentType,
                ContentDisposition = adPost.ContentDisposition
            };

            ad.file = file;

            if (ad.file.Length > 0)
            {
                if (!Directory.Exists(Path.Combine(_environnement.WebRootPath, folder)))
                {
                    Directory.CreateDirectory(Path.Combine(_environnement.WebRootPath, folder));
                }

                string filename = "_ad" + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss_") + ad.file.FileName;
                using (FileStream fileStream = System.IO.File.Create(Path.Combine(_environnement.WebRootPath, folder, filename)))
                {
                    ad.file.CopyTo(fileStream);
                    fileStream.Flush();
                    _context.Ads.Add(new Ad {
                        Id = ad.Id, Titre = ad.Titre, Description = ad.Description, AdView = folder + "/" + filename, Link = ad.Link
                    });
                    _context.SaveChanges();
                }

                await _context.SaveChangesAsync();
            }

            return(CreatedAtAction("GetAd", new { id = ad.Id }, ad));
        }
Ejemplo n.º 5
0
        private void CheckAllValues(
            ref ClientSession <AdId, NumClicks, AdInput, Output, Empty, SimpleFunctions> fht,
            int value)
        {
            AdInput inputArg  = default;
            Output  outputArg = default;

            for (var key = 0; key < numOps; key++)
            {
                inputArg.adId.adId = key;
                var status = fht.Read(ref inputArg.adId, ref inputArg, ref outputArg, Empty.Default, key);

                if (status == Status.PENDING)
                {
                    fht.CompletePending(true);
                }
                else
                {
                    Assert.IsTrue(outputArg.value.numClicks == value);
                }
            }

            fht.CompletePending(true);
        }
Ejemplo n.º 6
0
        private void CheckAllValues(
            ref ClientSession <AdId, NumClicks, AdInput, Output, Empty, AdSimpleFunctions> fht,
            int value)
        {
            AdInput inputArg  = default;
            Output  outputArg = default;

            for (var key = 0; key < numOps; key++)
            {
                inputArg.adId.adId = key;
                var status = fht.Read(ref inputArg.adId, ref inputArg, ref outputArg, Empty.Default, fht.SerialNo);

                if (status.IsPending)
                {
                    fht.CompletePending(true);
                }
                else
                {
                    Assert.AreEqual(value, outputArg.value.numClicks);
                }
            }

            fht.CompletePending(true);
        }
Ejemplo n.º 7
0
        public async Task AsyncRecoveryTest1(CheckpointType checkpointType)
        {
            TestUtils.DeleteDirectory(TestUtils.MethodTestDir, wait: true);
            log = Devices.CreateLogDevice(TestUtils.MethodTestDir + "/AsyncRecoveryTest1.log", deleteOnClose: true);

            string testPath = TestUtils.MethodTestDir + "/checkpoints4";

            Directory.CreateDirectory(testPath);

            fht1 = new FasterKV <AdId, NumClicks>
                       (128,
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, PageSizeBits = 10, MemorySizeBits = 13
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = testPath
            }
                       );

            fht2 = new FasterKV <AdId, NumClicks>
                       (128,
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, PageSizeBits = 10, MemorySizeBits = 13
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = testPath
            }
                       );

            int numOps     = 5000;
            var inputArray = new AdId[numOps];

            for (int i = 0; i < numOps; i++)
            {
                inputArray[i].adId = i;
            }

            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var s0 = fht1.For(functions).NewSession <AdSimpleFunctions>();
            var s1 = fht1.For(functions).NewSession <AdSimpleFunctions>();
            var s2 = fht1.For(functions).NewSession <AdSimpleFunctions>();

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                s1.Upsert(ref inputArray[key], ref value, Empty.Default, key);
            }

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                s2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, key);
            }

            // does not require session
            fht1.TryInitiateFullCheckpoint(out _, checkpointType);
            await fht1.CompleteCheckpointAsync();

            s2.CompletePending(true, false);

            fht1.TryInitiateFullCheckpoint(out Guid token, checkpointType);
            await fht1.CompleteCheckpointAsync();

            s2.Dispose();
            s1.Dispose();
            s0.Dispose();
            fht1.Dispose();

            fht2.Recover(token); // sync, does not require session

            using (var s3 = fht2.For(functions).ResumeSession <AdSimpleFunctions>(s1.ID, out CommitPoint lsn))
            {
                Assert.AreEqual(numOps - 1, lsn.UntilSerialNo);

                for (int key = 0; key < numOps; key++)
                {
                    var status = s3.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, s3.SerialNo);

                    if (status.IsPending)
                    {
                        s3.CompletePending(true, true);
                    }
                    else
                    {
                        Assert.AreEqual(key, output.value.numClicks);
                    }
                }
            }

            fht2.Dispose();
            log.Dispose();
            TestUtils.DeleteDirectory(TestUtils.MethodTestDir);
        }
Ejemplo n.º 8
0
        public async Task AsyncRecoveryTest1(CheckpointType checkpointType)
        {
            log = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "\\SimpleRecoveryTest2.log", deleteOnClose: true);

            Directory.CreateDirectory(TestContext.CurrentContext.TestDirectory + "\\checkpoints4");

            fht1 = new FasterKV
                   <AdId, NumClicks, AdInput, Output, Empty, SimpleFunctions>
                       (128, new SimpleFunctions(),
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, PageSizeBits = 10, MemorySizeBits = 13
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = TestContext.CurrentContext.TestDirectory + "\\checkpoints4", CheckPointType = checkpointType
            }
                       );

            fht2 = new FasterKV
                   <AdId, NumClicks, AdInput, Output, Empty, SimpleFunctions>
                       (128, new SimpleFunctions(),
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, PageSizeBits = 10, MemorySizeBits = 13
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = TestContext.CurrentContext.TestDirectory + "\\checkpoints4", CheckPointType = checkpointType
            }
                       );

            int numOps     = 5000;
            var inputArray = new AdId[numOps];

            for (int i = 0; i < numOps; i++)
            {
                inputArray[i].adId = i;
            }

            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var s0 = fht1.NewSession();
            var s1 = fht1.NewSession();
            var s2 = fht1.NewSession();

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                s1.Upsert(ref inputArray[key], ref value, Empty.Default, key);
            }

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                s2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, key);
            }

            // does not require session
            fht1.TakeFullCheckpoint(out _);
            await fht1.CompleteCheckpointAsync();

            s2.CompletePending(true);

            fht1.TakeFullCheckpoint(out Guid token);
            await fht1.CompleteCheckpointAsync();

            s2.Dispose();
            s1.Dispose();
            s0.Dispose();
            fht1.Dispose();

            fht2.Recover(token); // sync, does not require session

            var guid = s1.ID;

            using (var s3 = fht2.ResumeSession(guid, out CommitPoint lsn))
            {
                Assert.IsTrue(lsn.UntilSerialNo == numOps - 1);

                for (int key = 0; key < numOps; key++)
                {
                    var status = s3.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, 0);

                    if (status == Status.PENDING)
                    {
                        s3.CompletePending(true);
                    }
                    else
                    {
                        Assert.IsTrue(output.value.numClicks == key);
                    }
                }
            }

            fht2.Dispose();
            log.Close();
            new DirectoryInfo(TestContext.CurrentContext.TestDirectory + "\\checkpoints4").Delete(true);
        }
Ejemplo n.º 9
0
        private async ValueTask SimpleRecoveryTest1_Worker(CheckpointType checkpointType, ICheckpointManager checkpointManager, bool isAsync)
        {
            string checkpointDir = TestContext.CurrentContext.TestDirectory + $"/{TEST_CONTAINER}";

            if (checkpointManager != null)
            {
                checkpointDir = null;
            }

            log = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "/SimpleRecoveryTest1.log", deleteOnClose: true);

            fht1 = new FasterKV
                   <AdId, NumClicks>
                       (128,
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = checkpointDir, CheckpointManager = checkpointManager, CheckPointType = checkpointType
            }
                       );

            fht2 = new FasterKV
                   <AdId, NumClicks>
                       (128,
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = checkpointDir, CheckpointManager = checkpointManager, CheckPointType = checkpointType
            }
                       );


            int numOps     = 5000;
            var inputArray = new AdId[numOps];

            for (int i = 0; i < numOps; i++)
            {
                inputArray[i].adId = i;
            }

            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var session1 = fht1.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                session1.Upsert(ref inputArray[key], ref value, Empty.Default, 0);
            }
            fht1.TakeFullCheckpoint(out Guid token);
            fht1.CompleteCheckpointAsync().GetAwaiter().GetResult();
            session1.Dispose();

            if (isAsync)
            {
                await fht2.RecoverAsync(token);
            }
            else
            {
                fht2.Recover(token);
            }

            var session2 = fht2.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                var status = session2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, 0);

                if (status == Status.PENDING)
                {
                    session2.CompletePending(true);
                }
                else
                {
                    Assert.IsTrue(output.value.numClicks == key);
                }
            }
            session2.Dispose();

            log.Dispose();
            fht1.Dispose();
            fht2.Dispose();

            if (checkpointManager == null)
            {
                new DirectoryInfo(checkpointDir).Delete(true);
            }
        }
Ejemplo n.º 10
0
        public async ValueTask SimpleRecoveryTest2([Values] CheckpointType checkpointType, [Values] bool isAsync)
        {
            var checkpointManager = new DeviceLogCommitCheckpointManager(new LocalStorageNamedDeviceFactory(), new DefaultCheckpointNamingScheme(TestContext.CurrentContext.TestDirectory + "/checkpoints4"), false);

            log = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "/SimpleRecoveryTest2.log", deleteOnClose: true);

            // Directory.CreateDirectory(TestContext.CurrentContext.TestDirectory + "/checkpoints4");

            fht1 = new FasterKV
                   <AdId, NumClicks>
                       (128,
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointManager = checkpointManager, CheckPointType = checkpointType
            }
                       );

            fht2 = new FasterKV
                   <AdId, NumClicks>
                       (128,
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointManager = checkpointManager, CheckPointType = checkpointType
            }
                       );


            int numOps     = 5000;
            var inputArray = new AdId[numOps];

            for (int i = 0; i < numOps; i++)
            {
                inputArray[i].adId = i;
            }

            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var session1 = fht1.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                session1.Upsert(ref inputArray[key], ref value, Empty.Default, 0);
            }
            fht1.TakeFullCheckpoint(out Guid token);
            fht1.CompleteCheckpointAsync().GetAwaiter().GetResult();
            session1.Dispose();

            if (isAsync)
            {
                await fht2.RecoverAsync(token);
            }
            else
            {
                fht2.Recover(token);
            }

            var session2 = fht2.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                var status = session2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, 0);

                if (status == Status.PENDING)
                {
                    session2.CompletePending(true);
                }
                else
                {
                    Assert.IsTrue(output.value.numClicks == key);
                }
            }
            session2.Dispose();

            log.Dispose();
            fht1.Dispose();
            fht2.Dispose();
            checkpointManager.Dispose();

            new DirectoryInfo(TestContext.CurrentContext.TestDirectory + "/checkpoints4").Delete(true);
        }
Ejemplo n.º 11
0
        private async ValueTask SimpleRecoveryTest1_Worker(CheckpointType checkpointType, bool isAsync, bool testCommitCookie)
        {
            if (testCommitCookie)
            {
                // Generate a new unique byte sequence for test
                commitCookie = Guid.NewGuid().ToByteArray();
            }

            if (checkpointManager is null)
            {
                checkpointDir = TestUtils.MethodTestDir + $"/checkpoints";
            }

            log = Devices.CreateLogDevice(TestUtils.MethodTestDir + "/SimpleRecoveryTest1.log", deleteOnClose: true);

            fht1 = new FasterKV <AdId, NumClicks>(128,
                                                  logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                                                  checkpointSettings: new CheckpointSettings {
                CheckpointDir = checkpointDir, CheckpointManager = checkpointManager, CheckPointType = checkpointType
            }
                                                  );

            fht2 = new FasterKV <AdId, NumClicks>(128,
                                                  logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                                                  checkpointSettings: new CheckpointSettings {
                CheckpointDir = checkpointDir, CheckpointManager = checkpointManager, CheckPointType = checkpointType
            }
                                                  );

            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var session1 = fht1.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                session1.Upsert(ref inputArray[key], ref value, Empty.Default, 0);
            }

            if (testCommitCookie)
            {
                fht1.CommitCookie = commitCookie;
            }
            fht1.TakeFullCheckpoint(out Guid token);
            fht1.CompleteCheckpointAsync().GetAwaiter().GetResult();
            session1.Dispose();

            if (isAsync)
            {
                await fht2.RecoverAsync(token);
            }
            else
            {
                fht2.Recover(token);
            }

            if (testCommitCookie)
            {
                Assert.IsTrue(fht2.RecoveredCommitCookie.SequenceEqual(commitCookie));
            }
            else
            {
                Assert.Null(fht2.RecoveredCommitCookie);
            }

            var session2 = fht2.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                var status = session2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, 0);

                if (status == Status.PENDING)
                {
                    session2.CompletePending(true);
                }
                else
                {
                    Assert.IsTrue(output.value.numClicks == key);
                }
            }
            session2.Dispose();
        }
Ejemplo n.º 12
0
        public void SimpleRecoveryTest1(CheckpointType checkpointType)
        {
            log = Devices.CreateLogDevice(TestContext.CurrentContext.TestDirectory + "\\SimpleRecoveryTest1.log", deleteOnClose: true);

            Directory.CreateDirectory(TestContext.CurrentContext.TestDirectory + "\\checkpoints4");

            fht1 = new FasterKV
                   <AdId, NumClicks, AdInput, Output, Empty, SimpleFunctions>
                       (128, new SimpleFunctions(),
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = TestContext.CurrentContext.TestDirectory + "\\checkpoints4", CheckPointType = checkpointType
            }
                       );

            fht2 = new FasterKV
                   <AdId, NumClicks, AdInput, Output, Empty, SimpleFunctions>
                       (128, new SimpleFunctions(),
                       logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                       checkpointSettings: new CheckpointSettings {
                CheckpointDir = TestContext.CurrentContext.TestDirectory + "\\checkpoints4", CheckPointType = checkpointType
            }
                       );


            int numOps     = 5000;
            var inputArray = new AdId[numOps];

            for (int i = 0; i < numOps; i++)
            {
                inputArray[i].adId = i;
            }

            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var session1 = fht1.NewSession();

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                session1.Upsert(ref inputArray[key], ref value, Empty.Default, 0);
            }
            fht1.TakeFullCheckpoint(out Guid token);
            fht1.CompleteCheckpointAsync().GetAwaiter().GetResult();
            session1.Dispose();

            fht2.Recover(token);

            var session2 = fht2.NewSession();

            for (int key = 0; key < numOps; key++)
            {
                var status = session2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, 0);

                if (status == Status.PENDING)
                {
                    session2.CompletePending(true);
                }
                else
                {
                    Assert.IsTrue(output.value.numClicks == key);
                }
            }
            session2.Dispose();

            log.Close();
            fht1.Dispose();
            fht2.Dispose();
            new DirectoryInfo(TestContext.CurrentContext.TestDirectory + "\\checkpoints4").Delete(true);
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> PutAd(short id, AdPost adPost)
        {
            if (id != adPost.Id)
            {
                return(BadRequest());
            }

            string folder = "Ads";

            AdInput ad = new AdInput();

            ad.Id          = adPost.Id;
            ad.Titre       = adPost.Titre;
            ad.Description = adPost.Description;
            ad.Link        = adPost.Link;

            byte[]    data   = Convert.FromBase64String(adPost.file);
            var       stream = new MemoryStream(data);
            IFormFile file   = new FormFile(stream, 0, data.Length, adPost.name, adPost.fileName)
            {
                Headers            = new HeaderDictionary(),
                ContentType        = adPost.ContentType,
                ContentDisposition = adPost.ContentDisposition
            };

            ad.file = file;

            if (ad.file.Length > 0)
            {
                if (!Directory.Exists(Path.Combine(_environnement.WebRootPath, folder)))
                {
                    Directory.CreateDirectory(Path.Combine(_environnement.WebRootPath, folder));
                }

                string filename = "_ad" + DateTime.Now.ToString("_yyyy-MM-dd_HH-mm-ss_") + ad.file.FileName;
                using (FileStream fileStream = System.IO.File.Create(Path.Combine(_environnement.WebRootPath, folder, filename)))
                {
                    ad.file.CopyTo(fileStream);
                    fileStream.Flush();
                    _context.Entry(new Ad {
                        Id = ad.Id, Titre = ad.Titre, Description = ad.Description, AdView = folder + "/" + filename, Link = ad.Link
                    }).State = EntityState.Modified;
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AdExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 14
0
        public void SimpleReadAndUpdateInfoTest()
        {
            checkpointManager = new DeviceLogCommitCheckpointManager(new LocalStorageNamedDeviceFactory(), new DefaultCheckpointNamingScheme(TestUtils.MethodTestDir + "/checkpoints"), false);
            log = Devices.CreateLogDevice(TestUtils.MethodTestDir + "/SimpleReadAndUpdateInfoTest.log", deleteOnClose: true);

            fht1 = new FasterKV <AdId, NumClicks>(128,
                                                  logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                                                  checkpointSettings: new CheckpointSettings {
                CheckpointManager = checkpointManager
            }
                                                  );

            fht2 = new FasterKV <AdId, NumClicks>(128,
                                                  logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                                                  checkpointSettings: new CheckpointSettings {
                CheckpointManager = checkpointManager
            }
                                                  );


            NumClicks         value;
            AdInput           inputArg   = default;
            Output            output     = default;
            AdSimpleFunctions functions1 = new(1);
            AdSimpleFunctions functions2 = new(2);

            var session1 = fht1.NewSession(functions1);

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                if ((key & 1) > 0)
                {
                    session1.Upsert(ref inputArray[key], ref value, Empty.Default, 0);
                }
                else
                {
                    AdInput input = new() { adId = inputArray[key], numClicks = value };
                    session1.RMW(ref inputArray[key], ref input);
                }
            }
            fht1.TryInitiateFullCheckpoint(out Guid token, CheckpointType.FoldOver);
            fht1.CompleteCheckpointAsync().AsTask().GetAwaiter().GetResult();
            session1.Dispose();

            fht2.Recover(token);

            var session2 = fht2.NewSession(functions2);

            // Just need one operation here to verify readInfo/upsertInfo in the functions
            var lastKey = inputArray.Length - 1;
            var status  = session2.Read(ref inputArray[lastKey], ref inputArg, ref output, Empty.Default, 0);

            Assert.IsFalse(status.IsPending, status.ToString());

            value.numClicks = lastKey;
            status          = session2.Upsert(ref inputArray[lastKey], ref value, Empty.Default, 0);
            Assert.IsFalse(status.IsPending, status.ToString());

            inputArg = new() { adId = inputArray[lastKey], numClicks = new NumClicks {
                                   numClicks = 0
                               } };                                                                      // CopyUpdater adds, so make this 0
            status = session2.RMW(ref inputArray[lastKey], ref inputArg);
            Assert.IsFalse(status.IsPending, status.ToString());

            // Now verify Pending
            fht2.Log.FlushAndEvict(wait: true);

            output.value       = new() { numClicks = lastKey };
            inputArg.numClicks = new() { numClicks = lastKey };
            status             = session2.Read(ref inputArray[lastKey], ref inputArg, ref output, Empty.Default, 0);
            Assert.IsTrue(status.IsPending, status.ToString());
            session2.CompletePending(wait: true);

            // Upsert does not go pending so is skipped here

            --lastKey;
            output.value       = new() { numClicks = lastKey };
            inputArg.numClicks = new() { numClicks = lastKey };
            status             = session2.RMW(ref inputArray[lastKey], ref inputArg);
            Assert.IsTrue(status.IsPending, status.ToString());
            session2.CompletePending(wait: true);

            session2.Dispose();
        }
    }

    public class AdSimpleFunctions : FunctionsBase <AdId, NumClicks, AdInput, Output, Empty>
    {
        long expectedVersion;
Ejemplo n.º 15
0
        public async ValueTask SimpleRecoveryTest2([Values] CheckpointType checkpointType, [Values] bool isAsync)
        {
            checkpointManager = new DeviceLogCommitCheckpointManager(new LocalStorageNamedDeviceFactory(), new DefaultCheckpointNamingScheme(TestUtils.MethodTestDir + "/checkpoints4"), false);
            log = Devices.CreateLogDevice(TestUtils.MethodTestDir + "/SimpleRecoveryTest2.log", deleteOnClose: true);

            fht1 = new FasterKV <AdId, NumClicks>(128,
                                                  logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                                                  checkpointSettings: new CheckpointSettings {
                CheckpointManager = checkpointManager
            }
                                                  );

            fht2 = new FasterKV <AdId, NumClicks>(128,
                                                  logSettings: new LogSettings {
                LogDevice = log, MutableFraction = 0.1, MemorySizeBits = 29
            },
                                                  checkpointSettings: new CheckpointSettings {
                CheckpointManager = checkpointManager
            }
                                                  );


            NumClicks value;
            AdInput   inputArg = default;
            Output    output   = default;

            var session1 = fht1.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                value.numClicks = key;
                session1.Upsert(ref inputArray[key], ref value, Empty.Default, 0);
            }
            fht1.TryInitiateFullCheckpoint(out Guid token, checkpointType);
            fht1.CompleteCheckpointAsync().AsTask().GetAwaiter().GetResult();
            session1.Dispose();

            if (isAsync)
            {
                await fht2.RecoverAsync(token);
            }
            else
            {
                fht2.Recover(token);
            }

            var session2 = fht2.NewSession(new AdSimpleFunctions());

            for (int key = 0; key < numOps; key++)
            {
                var status = session2.Read(ref inputArray[key], ref inputArg, ref output, Empty.Default, 0);

                if (status.IsPending)
                {
                    session2.CompletePending(true);
                }
                else
                {
                    Assert.AreEqual(key, output.value.numClicks);
                }
            }
            session2.Dispose();
        }