private void HandleInitiateMigration(InitiateMigration message)
 {
     //Case Docker
     if (ContainerTable.ContainerType == ContainersType.D)
     {
         var container = ContainerTable.GetContainerById(message.ContainerId) as DockerContainer;
         var imageId   = container.ImageId;
         var request   = new ImageLoadRequest(message.TargetHost, this.MachineId, imageId, message.ContainerId);
         CommunicationModule.SendMessage(request);
     }
     //Case Normal
     else
     {
         Task t = new Task(() =>
         {
             var container = ContainerTable.GetContainerById(message.ContainerId);
             ContainerTable.LockContainer(message.ContainerId);
             container.Checkpoint(this.MachineId);
             var size = (int)container.GetContainerNeededLoadInfo().CurrentLoad.MemorySize;
             MigrateContainerRequest request =
                 new MigrateContainerRequest(message.TargetHost, this.MachineId, container, size);
             CommunicationModule.SendMessage(request);
             ResetBackOff();
         });
         t.Start();
     }
     //GlobalEnviromentVariables.ResetCheckRate();
 }
    protected void bind_container(string mode)
    {
        //have to use a collection as formview requires i IListSource, IEnumerable, or IDataSource.
        ContainerTableCollection _tbc = new ContainerTableCollection();

        if (mode != "Insert")
        {
            //get key id
            string _pid = get_token("pid");
            if(_pid != null)
            {
                int _containerid = wwi_func.vint(wwi_security.DecryptString(_pid, "publiship"));
                ContainerTable _tbl = new ContainerTable(_containerid);
                _tbc.Add(_tbl);
            }
            else
            {
                string[] _args = {  System.IO.Path.GetDirectoryName(Page.AppRelativeVirtualPath),
                                                "container_search",};
                string _url = string.Format("{0}\\{1}.aspx?", _args);
                Response.Redirect(_url);
            }
        }
        else
        {
            ContainerTable _tbl = new ContainerTable();
            _tbc.Add(_tbl);
        }
        
        //bind formview to collection
        this.fmvContainer.DataSource = _tbc;
        this.fmvContainer.DataBind(); 
    }
        private void CompareAndBalance()
        {
            var hosts = _commonLoadManager.GetAllHostLoadInfos();
            Dictionary <int, double> pdis = CalculatePropDistributionForAllHosts(hosts);
            Random r = new Random(Guid.NewGuid().GetHashCode());
            List <ContainerToHost> list = new List <ContainerToHost>();

            foreach (var container in ContainerTable.GetAllContainers())
            {
                var k     = r.GetRandomFromDictionary(pdis);
                var cdash = _commonLoadManager.GetHostLoadInfoByHostIdAfterContainer(k, container.GetContainerNeededLoadInfo()).Volume;//Should be after adding the current container
                var c     = LoadManager.GetNeededHostLoadInfo().Volume;
                if (cdash < c)
                {
                    list.Add(new ContainerToHost(container.ContainerId, k, c - cdash));
                }
            }
            if (list.Count == 0)
            {
                return;
            }
            var total = list.Select(x => x.Cost).Sum();

            foreach (var item in list)
            {
                item.Probaility = item.Cost / total;
            }

            var result = r.GetRandomFromContainerToHost(list);

            MigrationContainer(result);
            Busy = true;
        }
 public HostHandlerModule(NetworkInterfaceCard communicationModule, ContainerTable containerTable, ILoadManager loadManager)
 {
     MachineId           = communicationModule.MachineId;
     ContainerTable      = containerTable;
     LoadManager         = loadManager;
     CommunicationModule = communicationModule;
 }
Example #5
0
        private void HandlePullRequest(ForsmanPullRequest message)
        {
            ForsmanLoadAvailabilityResponce responce;

            if (BidLock == -1)
            {
                BidLock = message.SenderId;
                List <ForsmanBid> bids = new List <ForsmanBid>();
                var load = LoadManager.GetPredictedHostLoadInfo();

                foreach (var cont in ContainerTable.GetAllContainers())
                {
                    var conload = cont.GetContainerPredictedLoadInfo();
                    var nload   = LoadManager.GetHostLoadInfoAWithoutContainer(conload);
                    if (LoadManager.CanITakeLoad(conload))
                    //&& nload.CalculateTotalUtilizationState(MinUtilization, MaxUtilization) != UtilizationStates.UnderUtilization)
                    {
                        ForsmanBid bid = new ForsmanBid(MachineId, true, nload, message.AuctionId, conload.ContainerId,
                                                        BidReasons.ValidBid, conload);
                        bids.Add(bid);
                    }
                }
                responce = new ForsmanLoadAvailabilityResponce(message.SenderId, this.MachineId, load, true, bids);
            }
            else
            {
                responce = new ForsmanLoadAvailabilityResponce(message.SenderId, this.MachineId, null, false, null);
                //Resources are locked in the first place
            }
            CommunicationModule.SendMessage(responce);
        }
Example #6
0
 public ForsmanHostHandler(NetworkInterfaceCard communicationModule, ContainerTable containerTable, ILoadManager loadManager, StrategyActionType currentActionType,
                           SimulationSize simulationSize) : base(communicationModule, containerTable, loadManager)
 {
     _currentActionType = currentActionType;
     SimulationSize     = simulationSize;
     MaxUtilization     = Global.OtherMaxUtilization;
     MinUtilization     = Global.OtherMinUtilization;
 }
Example #7
0
        public Container GetPlacementContainer(GrabFlag flag)
        {
            if (ContainerTable.ContainsKey(flag))
            {
                return(ContainerTable[flag]);
            }

            return(null);
        }
Example #8
0
 public HostLoadManager(int machineId, Load maxLoad, LoadPrediction loadPrediction, NetworkInterfaceCard networkCard, ContainerTable containerTable)
 {
     _maxLoad              = maxLoad;
     _networkCard          = networkCard;
     _osLoad               = new Load(maxLoad.CpuLoad * 0.01, 100, 0);
     _containerTable       = containerTable;
     MachineId             = machineId;
     CurrentLoadPrediction = loadPrediction;
 }
 public void AfterDeserializedRows(ContainerTable table, SyncTable schemaTable, IConverter converter)
 {
     if (table.Rows.Count > 0)
     {
         foreach (var row in table.Rows)
         {
             converter.AfterDeserialized(row, schemaTable);
         }
     }
 }
 public void BeforeSerializeRows(ContainerTable table, SyncTable schemaTable, IConverter converter)
 {
     if (table.Rows.Count > 0)
     {
         foreach (var row in table.Rows)
         {
             converter.BeforeSerialize(row, schemaTable);
         }
     }
 }
        private void HanleAddContainerRequest(AddContainerRequest addContainerRequest)
        {
            Task t = new Task(async() => {
                var table     = ContainerTable as DockerContainerTable;
                var dockerCon = addContainerRequest.NewContainer as DockerContainer;
                await table.LoadImage(dockerCon.ImageId);
                ContainerTable.AddContainer(addContainerRequest.NewContainer.ContainerId, addContainerRequest.NewContainer);
            });

            t.Start();
        }
Example #12
0
        private void HandleInitiateMigrationRequest(InitiateMigration message)
        {
            var con  = ContainerTable.GetContainerById(message.ContainerId);
            var size = (int)con.GetContainerNeededLoadInfo().CurrentLoad.MemorySize;

            ContainerTable.LockContainer(con.ContainerId);
            con.Checkpoint(this.MachineId);
            MigrateContainerRequest request =
                new MigrateContainerRequest(message.SenderId, this.MachineId, con, size);

            CommunicationModule.SendMessage(request);
        }
        private void MigrationContainer(ContainerToHost result)
        {
            var con  = ContainerTable.GetContainerById(result.ConId);
            var size = (int)con.GetContainerNeededLoadInfo().CurrentLoad.MemorySize;

            ContainerTable.LockContainer(con.ContainerId);
            con.Checkpoint(this.MachineId);

            MigrateContainerRequest request =
                new MigrateContainerRequest(result.HostId, this.MachineId, con, size);

            CommunicationModule.SendMessage(request);
        }
        /// <summary>
        /// container selection by condition
        /// </summary>
        /// <returns></returns>
        protected ContainerLoadInfo GetToBeRemovedContainerLoadInfo()
        {
            var r = ContainerTable.SelectContainerByCondition();

            if (r == null)
            {
                return(null);
            }
            else
            {
                return(r.GetContainerPredictedLoadInfo());
            }
        }
Example #15
0
 private void HandleMigrateContainerResponce(MigrateContainerResponse message)
 {
     if (message.Done)
     {
         ContainerTable.FreeLockedContainer();
         //_containersTable.Remove(sendContainerResponce.ContainerId);
     }
     else
     {
         ContainerTable.UnLockContainer();
     }
     //Release Lock
     BidLock = -1;
 }
        public HostMachine(int id,
                           Load maxLoad, NetworkSwitch networkSwitch,
                           LoadPrediction currentLoadPrediction, Strategies strategy,
                           ContainersType containerType,
                           SimulationSize simulationSize) : base(id, networkSwitch)
        {
            if (containerType == ContainersType.D)
            {
                _containerTable = new DockerContainerTable(id, new ImageManager(this.CommunicationModule));
            }
            else
            {
                _containerTable = new ContainerTable(id);
            }
            _loadManager = new HostLoadManager(this.MachineId, maxLoad, currentLoadPrediction, this.CommunicationModule, _containerTable);
            switch (strategy)
            {
            case Strategies.WAshraf2017Auction:
                _handler = new WAshrafHostHandlerModule(CommunicationModule, _containerTable, _loadManager);

                break;

            case Strategies.WAshraf2017:
                _handler = new WAshrafHostHandlerModule(CommunicationModule, _containerTable, _loadManager);

                break;

            case Strategies.Zhao:
                _handler = new ZhaoHostHandler(CommunicationModule, _containerTable, _loadManager, Global.CommonLoadManager);

                break;

            case Strategies.ForsmanPush:
                _handler = new ForsmanHostHandler(CommunicationModule, _containerTable, _loadManager, StrategyActionType.PushAction, simulationSize);

                break;

            case Strategies.ForsmanPull:
                _handler = new ForsmanHostHandler(CommunicationModule, _containerTable, _loadManager, StrategyActionType.PullAction, simulationSize);
                break;

            case Strategies.Proposed2018:
                _handler = new ProposedHostHandlerModule(CommunicationModule, _containerTable, _loadManager);
                break;

            default:

                throw new ArgumentOutOfRangeException(nameof(strategy), strategy, null);
            }
        }
 private void HandleMigrateContainerResponce(MigrateContainerResponse message)
 {
     if (message.Done)
     {
         ContainerTable.FreeLockedContainer();
         //_containersTable.Remove(sendContainerResponce.ContainerId);
     }
     else
     {
         ContainerTable.UnLockContainer();
     }
     //Release Lock
     _hostState.CurrentAction = HostCurrentAction.None;
 }
Example #18
0
        private void HandleLoadAvailabilityResponse(ForsmanLoadAvailabilityResponce message)
        {
            _currentAuction.EndWaitFor(message.SenderId, message.OldLoadInfo);
            if (message.Valid)
            {
                foreach (var bid in message.Bids)
                {
                    _currentAuction.AddBid(bid, MinUtilization, MaxUtilization);
                }
            }

            if (!_currentAuction.OpenSession)
            {
                var winner = _currentAuction.GetWinnerBid();
                if (winner != null)
                {
                    WinnerAnnouncementMessage responce = new WinnerAnnouncementMessage(-1, this.MachineId, winner.BiddingHost);
                    CommunicationModule.SendMessage(responce);
                    if (_currentActionType == StrategyActionType.PushAction)
                    {
                        var con  = ContainerTable.GetContainerById(winner.ContainerId);
                        var size = (int)con.GetContainerNeededLoadInfo().CurrentLoad.MemorySize;
                        ContainerTable.LockContainer(con.ContainerId);
                        con.Checkpoint(this.MachineId);
                        MigrateContainerRequest request =
                            new MigrateContainerRequest(winner.BiddingHost, this.MachineId, con, size);
                        CommunicationModule.SendMessage(request);
                    }
                    else
                    {
                        InitiateMigration request = new InitiateMigration(this.MachineId, winner.BiddingHost, winner.BiddingHost, winner.ContainerId);
                        CommunicationModule.SendMessage(request);
                    }
                    ResetBackOff();
                    //_lastDelay = 5;
                }
                else //No winner
                {
                    WinnerAnnouncementMessage responce = new WinnerAnnouncementMessage(-1, this.MachineId, 0);
                    CommunicationModule.SendMessage(responce);
                    MaxUtilization = Global.OtherMaxUtilization;
                    MinUtilization = Global.OtherMinUtilization;
                    IncreaseBackOffTime();
                    BidLock = -1;
                    //_lastDelay = 5;
                }
                _currentAuction = null;
            }
        }
Example #19
0
        private void HandleMigrateContainerRequest(MigrateContainerRequest message)
        {
            Task t = new Task(() =>
            {
                ContainerTable.AddContainer(message.MigratedContainer.ContainerId, message.MigratedContainer);
                message.MigratedContainer.Restore(this.MachineId);
                var responce =
                    new MigrateContainerResponse(message.SenderId, this.MachineId, message.MigratedContainer.ContainerId,
                                                 true);
                CommunicationModule.SendMessage(responce);
                BidLock = -1;
                ResetBackOff();
            });

            t.Start();
        }
	    public void Insert(string ContainerNumber,int? SizeTypeID,int? VoyageID,int? OriginPortID,int? DestinationPortID,int? Cycfs,DateTime? DevanDate,int? DevanWarehouseID,bool? Devanned,DateTime? DeliveryDate,bool? Delivered,bool? LoadedOnBoard,DateTime? Updated,string DevanNotes,int? OriginControllerID,int? DestinationControllerID,DateTime? DeliveryTime,string BookingRef,int? ContainerStatusID,byte[] Ts)
	    {
		    ContainerTable item = new ContainerTable();
		    
            item.ContainerNumber = ContainerNumber;
            
            item.SizeTypeID = SizeTypeID;
            
            item.VoyageID = VoyageID;
            
            item.OriginPortID = OriginPortID;
            
            item.DestinationPortID = DestinationPortID;
            
            item.Cycfs = Cycfs;
            
            item.DevanDate = DevanDate;
            
            item.DevanWarehouseID = DevanWarehouseID;
            
            item.Devanned = Devanned;
            
            item.DeliveryDate = DeliveryDate;
            
            item.Delivered = Delivered;
            
            item.LoadedOnBoard = LoadedOnBoard;
            
            item.Updated = Updated;
            
            item.DevanNotes = DevanNotes;
            
            item.OriginControllerID = OriginControllerID;
            
            item.DestinationControllerID = DestinationControllerID;
            
            item.DeliveryTime = DeliveryTime;
            
            item.BookingRef = BookingRef;
            
            item.ContainerStatusID = ContainerStatusID;
            
            item.Ts = Ts;
            
	    
		    item.Save(UserName);
	    }
        private void HandleMigrateContainerResponse(MigrateContainerResponse message)
        {
            if (message.Done)
            {
                ContainerTable.FreeLockedContainer();
                Busy = false;
                //ResetBackOff();

                //_containersTable.Remove(sendContainerResponce.ContainerId);
            }
            else
            {
                throw new NotImplementedException("responce");
                //_containerTable.UnLockContainer();
            }
            //BidLock = -1;
        }
        private void HandleMigrateContainerRequest(MigrateContainerRequest message)
        {
            message.MigratedContainer.Restore(this.MachineId);
            if (message.MigratedContainer.ContainerType == ContainersType.D)
            {
                var table     = ContainerTable as DockerContainerTable;
                var container = message.MigratedContainer as DockerContainer;
                var t         = table.LoadImage(container.ImageId);
                t.Wait();
            }
            ContainerTable.AddContainer(message.MigratedContainer.ContainerId, message.MigratedContainer);
            var responce =
                new MigrateContainerResponse(message.SenderId, this.MachineId, message.MigratedContainer.ContainerId,
                                             true);

            CommunicationModule.SendMessage(responce);
        }
Example #23
0
        private void HandleInitiateMigration(InitiateMigration message)
        {
            //Console.WriteLine($" \n\n\n\n \t\t\t From Push with Love{message.ContainerId}");
            Task t = new Task(() =>
            {
                var container = ContainerTable.GetContainerById(message.ContainerId);
                ContainerTable.LockContainer(message.ContainerId);
                container.Checkpoint(this.MachineId);
                var size = (int)container.GetContainerNeededLoadInfo().CurrentLoad.MemorySize;
                MigrateContainerRequest request = new MigrateContainerRequest(message.TargetHost, this.MachineId,
                                                                              container, size);
                CommunicationModule.SendMessage(request);
                ResetBackOff();
            });

            t.Start();
            //GlobalEnviromentVariables.ResetCheckRate();
        }
Example #24
0
        protected override bool SendPushRequest()
        {
            var list = ContainerTable.GetAllContainersLoadInfo();
            //.Where(x=>
            //_loadManager.GetHostLoadInfoAWithoutContainer(x)
            //.CalculateTotalUtilizationState(MinUtilization,MaxUtilization)!= UtilizationStates.UnderUtilization )
            //.ToList();
            int total = (int)SimulationSize - 1;
            var load  = LoadManager.GetPredictedHostLoadInfo();

            _currentAuction = new ForsmanPushAuction(load, list, total);
            BidLock         = 0;
            ForsmanPushRequest m = new ForsmanPushRequest(-1, this.MachineId, load, this.MachineId, list);

            CommunicationModule.SendMessage(m);
            Console.WriteLine($"Host No{MachineId} send Push Request");
            return(false);
        }
Example #25
0
 private void BuildTable(IEnumerable <Product> products)
 {
     foreach (var prod in products)
     {
         ContainerTable.Add(
             new Product
         {
             Id           = prod.Id,
             Name         = prod.Name,
             Description  = prod.Description,
             Quantity     = prod.Quantity,
             Status       = prod.Status,
             Unit         = prod.Unit,
             CategoryId   = prod.CategoryId,
             CategoryName = prod.CategoryName,
         });
     }
 }
        private void HandleMigrateContainerRequest(MigrateContainerRequest message)
        {
            Task t = new Task(() =>
            {
                ContainerTable.AddContainer(message.MigratedContainer.ContainerId, message.MigratedContainer);
                //var nd = Global.GetNetworkDelay(message.MessageSize);
                //await Task.Delay(nd*Global.Second);
                message.MigratedContainer.Restore(this.MachineId);
                var responce =
                    new MigrateContainerResponse(message.SenderId, this.MachineId, message.MigratedContainer.ContainerId,
                                                 true);
                CommunicationModule.SendMessage(responce);
                _hostState.CurrentAction = HostCurrentAction.None;
                ResetBackOff();
            });

            t.Start();
        }
 private void HandleImageLoadResponce(ImageLoadResponce message)
 {
     if (message.State)
     {
         Task t = new Task(() =>
         {
             var container = ContainerTable.GetContainerById(message.ContainerId);
             ContainerTable.LockContainer(message.ContainerId);
             container.Checkpoint(this.MachineId);
             var size = (int)container.GetContainerNeededLoadInfo().CurrentLoad.MemorySize;
             MigrateContainerRequest request =
                 new MigrateContainerRequest(message.SenderId, this.MachineId, container, size);
             CommunicationModule.SendMessage(request);
             ResetBackOff();
         });
         t.Start();
     }
     else
     {
         throw new Exception("How Come its not loaded");
     }
     //GlobalEnviromentVariables.ResetCheckRate();
 }
    protected void bind_container(string mode)
    {
        //have to use a collection as formview requires i IListSource, IEnumerable, or IDataSource.
        ContainerTableCollection _tbc = new ContainerTableCollection();

        if (mode != "Insert")
        {
            //get key id
            int _containerid = wwi_func.vint(wwi_security.DecryptString(get_token("pid").ToString(), "publiship"));

            ContainerTable _tbl = new ContainerTable(_containerid);
            _tbc.Add(_tbl);
        }
        else
        {
            ContainerTable _tbl = new ContainerTable();
            _tbc.Add(_tbl);
        }

        //bind formview to collection
        this.fmvContainer.DataSource = _tbc;
        this.fmvContainer.DataBind();
    }
    //end update

    protected int insert_container()
    {
        int _newid = 0;
        //for nullable values
        int? _intnull = null;
        DateTime? _dtnull = null;

        try
        {
            ContainerTable _tbl = new ContainerTable();
 
            ASPxTextBox _txt = (ASPxTextBox)this.fmvContainer.FindControl("dxtxtContainerNo");
            if (_txt != null) { _tbl.ContainerNumber = _txt.Text.ToString(); }

            ASPxComboBox _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboContainerStatus");
            if (_cbo != null) { _tbl.ContainerStatusID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; ; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboContainerType");
            if (_cbo != null) { _tbl.SizeTypeID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; ; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboVoyage");
            if (_cbo != null) { _tbl.VoyageID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboOriginPort");
            if (_cbo != null) { _tbl.OriginPortID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboDestPort");
            if (_cbo != null) { _tbl.DestinationPortID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboTerms");
            if (_cbo != null) { _tbl.Cycfs = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboWarehouse");
            if (_cbo != null) { _tbl.DevanWarehouseID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboOriginController");
            if (_cbo != null) { _tbl.OriginControllerID = _cbo.Value != null ? wwi_func.vint(_cbo.Value.ToString()) : _intnull; }

            _cbo = (ASPxComboBox)this.fmvContainer.FindControl("dxcboDestController");
            if (_cbo != null) { _tbl.DestinationControllerID = _cbo.Value != null? wwi_func.vint(_cbo.Value.ToString()): _intnull; }

            ASPxMemo _mem = (ASPxMemo)this.fmvContainer.FindControl("dxmemDevan");
            if (_mem != null) { _tbl.DevanNotes  = _mem.Text.ToString(); }

            ASPxCheckBox _ckb = (ASPxCheckBox)this.fmvContainer.FindControl("dxckDelivered");
            if (_ckb != null) { _tbl.Delivered = _ckb.Value != null? wwi_func.vbool(_ckb.Value.ToString()): false; }

            ASPxDateEdit _dte = (ASPxDateEdit)this.fmvContainer.FindControl("dxdtDeliveryDate");
            if (_dte != null) { _tbl.DeliveryDate = _dte.Value != null ? wwi_func.vdatetime(_dte.Value.ToString()) : _dtnull; }

            _ckb = (ASPxCheckBox)this.fmvContainer.FindControl("dxckDevanned");
            if (_ckb != null) { _tbl.Devanned = _ckb.Value != null? wwi_func.vbool(_ckb.Value.ToString()): false; }

            _dte = (ASPxDateEdit)this.fmvContainer.FindControl("dxdtDevanDate");
            if (_dte != null) { _tbl.DevanDate = _dte.Value != null ? wwi_func.vdatetime(_dte.Value.ToString()) : _dtnull; }

            _tbl.Save();

            //get new id
            _newid = (int)_tbl.GetPrimaryKeyValue(); 
        }
        catch (Exception _ex)
        {
            this.dxlblErr.Text = _ex.Message.ToString();
            this.dxpnlErr.ClientVisible = true;
        }

        return _newid;
    }
Example #30
0
        protected override void SendPullRequest()
        {
            Console.WriteLine($"I'm Host #{MachineId} and I am pulling a container and I have {ContainerTable.GetContainersCount()} contatiners");

            BidLock = 0;
            PullRequest pullRequest = new PullRequest(0, this.MachineId, LoadManager.GetPredictedHostLoadInfo());

            CommunicationModule.SendMessage(pullRequest);
        }
        InternalApplyThenGetChangesAsync(ClientScopeInfo clientScopeInfo, SyncContext context, BatchInfo clientBatchInfo, DbConnection connection = default, DbTransaction transaction = default, CancellationToken cancellationToken = default, IProgress <ProgressArgs> progress = null)
        {
            await using var runner = await this.GetConnectionAsync(context, SyncMode.Reading, SyncStage.ChangesApplying, connection, transaction, cancellationToken, progress).ConfigureAwait(false);

            SyncSet         schema;
            ServerScopeInfo serverScopeInfo;

            // is it something that could happens ?
            if (clientScopeInfo.Schema == null)
            {
                // Make a remote call to get Schema from remote provider
                (context, serverScopeInfo) = await this.InternalGetServerScopeInfoAsync(
                    context, null, runner.Connection, runner.Transaction, runner.CancellationToken, runner.Progress).ConfigureAwait(false);

                schema = serverScopeInfo.Schema;
            }
            else
            {
                schema = clientScopeInfo.Schema;
            }

            schema.EnsureSchema();

            // if we don't have any BatchPartsInfo, just generate a new one to get, at least, something to send to the server
            // and get a response with new data from server
            if (clientBatchInfo == null)
            {
                clientBatchInfo = new BatchInfo(schema);
            }

            // --------------------------------------------------------------
            // STEP 1 : Send everything to the server side
            // --------------------------------------------------------------

            HttpResponseMessage response = null;

            // If not in memory and BatchPartsInfo.Count == 0, nothing to send.
            // But we need to send something, so generate a little batch part
            if (clientBatchInfo.BatchPartsInfo.Count == 0)
            {
                var changesToSend = new HttpMessageSendChangesRequest(context, clientScopeInfo);

                var containerSet = new ContainerSet();
                changesToSend.Changes     = containerSet;
                changesToSend.IsLastBatch = true;
                changesToSend.BatchIndex  = 0;
                changesToSend.BatchCount  = clientBatchInfo.BatchPartsInfo == null ? 0 : clientBatchInfo.BatchPartsInfo.Count;
                var inMemoryRowsCount = changesToSend.Changes.RowsCount();

                context.ProgressPercentage += 0.125;

                await this.InterceptAsync(new HttpSendingClientChangesRequestArgs(changesToSend, inMemoryRowsCount, inMemoryRowsCount, this.GetServiceHost()), progress, cancellationToken).ConfigureAwait(false);

                // serialize message
                var serializer = this.SerializerFactory.GetSerializer <HttpMessageSendChangesRequest>();
                var binaryData = await serializer.SerializeAsync(changesToSend);

                response = await this.httpRequestHandler.ProcessRequestAsync
                               (this.HttpClient, context, this.ServiceUri, binaryData, HttpStep.SendChangesInProgress,
                               this.SerializerFactory, this.Converter, this.Options.BatchSize, this.SyncPolicy, cancellationToken, progress).ConfigureAwait(false);
            }
            else
            {
                int tmpRowsSendedCount = 0;

                // Foreach part, will have to send them to the remote
                // once finished, return context
                var initialPctProgress1 = context.ProgressPercentage;
                var localSerializer     = new LocalJsonSerializer();

                var interceptorsReading = this.interceptors.GetInterceptors <DeserializingRowArgs>();
                if (interceptorsReading.Count > 0)
                {
                    localSerializer.OnReadingRow(async(schemaTable, rowString) =>
                    {
                        var args = new DeserializingRowArgs(context, schemaTable, rowString);
                        await this.InterceptAsync(args);
                        return(args.Result);
                    });
                }
                foreach (var bpi in clientBatchInfo.BatchPartsInfo.OrderBy(bpi => bpi.Index))
                {
                    // Get the updatable schema for the only table contained in the batchpartinfo
                    var schemaTable = DbSyncAdapter.CreateChangesTable(schema.Tables[bpi.Tables[0].TableName, bpi.Tables[0].SchemaName]);

                    // Generate the ContainerSet containing rows to send to the user
                    var containerSet   = new ContainerSet();
                    var containerTable = new ContainerTable(schemaTable);
                    var fullPath       = Path.Combine(clientBatchInfo.GetDirectoryFullPath(), bpi.FileName);
                    containerSet.Tables.Add(containerTable);

                    // read rows from file
                    foreach (var row in localSerializer.ReadRowsFromFile(fullPath, schemaTable))
                    {
                        containerTable.Rows.Add(row.ToArray());
                    }

                    // Call the converter if needed
                    if (this.Converter != null && containerTable.HasRows)
                    {
                        BeforeSerializeRows(containerTable, schemaTable, this.Converter);
                    }

                    // Create the send changes request
                    var changesToSend = new HttpMessageSendChangesRequest(context, clientScopeInfo)
                    {
                        Changes     = containerSet,
                        IsLastBatch = bpi.IsLastBatch,
                        BatchIndex  = bpi.Index,
                        BatchCount  = clientBatchInfo.BatchPartsInfo.Count
                    };

                    tmpRowsSendedCount += containerTable.Rows.Count;

                    context.ProgressPercentage = initialPctProgress1 + ((changesToSend.BatchIndex + 1) * 0.2d / changesToSend.BatchCount);
                    await this.InterceptAsync(new HttpSendingClientChangesRequestArgs(changesToSend, tmpRowsSendedCount, clientBatchInfo.RowsCount, this.GetServiceHost()), progress, cancellationToken).ConfigureAwait(false);

                    // serialize message
                    var serializer = this.SerializerFactory.GetSerializer <HttpMessageSendChangesRequest>();
                    var binaryData = await serializer.SerializeAsync(changesToSend);

                    response = await this.httpRequestHandler.ProcessRequestAsync
                                   (this.HttpClient, context, this.ServiceUri, binaryData, HttpStep.SendChangesInProgress,
                                   this.SerializerFactory, this.Converter, this.Options.BatchSize, this.SyncPolicy, cancellationToken, progress).ConfigureAwait(false);

                    // See #721 for issue and #721 for PR from slagtejn
                    if (!bpi.IsLastBatch)
                    {
                        response.Dispose();
                    }
                }
            }

            // --------------------------------------------------------------
            // STEP 2 : Receive everything from the server side
            // --------------------------------------------------------------

            // Now we have sent all the datas to the server and now :
            // We have a FIRST response from the server with new datas
            // 1) Could be the only one response
            // 2) Could be the first response and we need to download all batchs

            context.SyncStage = SyncStage.ChangesSelecting;
            var initialPctProgress = 0.55;

            context.ProgressPercentage = initialPctProgress;

            // Create the BatchInfo
            var serverBatchInfo = new BatchInfo(schema);

            HttpMessageSummaryResponse summaryResponseContent = null;

            // Deserialize response incoming from server
            using (var streamResponse = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
            {
                var responseSerializer = this.SerializerFactory.GetSerializer <HttpMessageSummaryResponse>();
                summaryResponseContent = await responseSerializer.DeserializeAsync(streamResponse);
            }

            serverBatchInfo.RowsCount = summaryResponseContent.BatchInfo.RowsCount;
            serverBatchInfo.Timestamp = summaryResponseContent.RemoteClientTimestamp;
            context = summaryResponseContent.SyncContext;

            if (summaryResponseContent.BatchInfo.BatchPartsInfo != null)
            {
                foreach (var bpi in summaryResponseContent.BatchInfo.BatchPartsInfo)
                {
                    serverBatchInfo.BatchPartsInfo.Add(bpi);
                }
            }


            // From here, we need to serialize everything on disk

            // Generate the batch directory
            var batchDirectoryRoot = this.Options.BatchDirectory;
            var batchDirectoryName = string.Concat(DateTime.UtcNow.ToString("yyyy_MM_dd_ss"), Path.GetRandomFileName().Replace(".", ""));

            serverBatchInfo.DirectoryRoot = batchDirectoryRoot;
            serverBatchInfo.DirectoryName = batchDirectoryName;

            if (!Directory.Exists(serverBatchInfo.GetDirectoryFullPath()))
            {
                Directory.CreateDirectory(serverBatchInfo.GetDirectoryFullPath());
            }

            // If we have a snapshot we are raising the batches downloading process that will occurs
            await this.InterceptAsync(new HttpBatchesDownloadingArgs(context, serverBatchInfo, this.GetServiceHost()), progress, cancellationToken).ConfigureAwait(false);

            // function used to download one part
            var dl = new Func <BatchPartInfo, Task>(async(bpi) =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var changesToSend3 = new HttpMessageGetMoreChangesRequest(context, bpi.Index);

                var serializer3 = this.SerializerFactory.GetSerializer <HttpMessageGetMoreChangesRequest>();
                var binaryData3 = await serializer3.SerializeAsync(changesToSend3).ConfigureAwait(false);
                var step3       = HttpStep.GetMoreChanges;

                await this.InterceptAsync(new HttpGettingServerChangesRequestArgs(bpi.Index, serverBatchInfo.BatchPartsInfo.Count, summaryResponseContent.SyncContext, this.GetServiceHost()), progress, cancellationToken).ConfigureAwait(false);

                // Raise get changes request
                context.ProgressPercentage = initialPctProgress + ((bpi.Index + 1) * 0.2d / serverBatchInfo.BatchPartsInfo.Count);

                var response = await this.httpRequestHandler.ProcessRequestAsync(
                    this.HttpClient, context, this.ServiceUri, binaryData3, step3,
                    this.SerializerFactory, this.Converter, 0, this.SyncPolicy, cancellationToken, progress).ConfigureAwait(false);

                if (this.SerializerFactory.Key != "json")
                {
                    var webSerializer        = this.SerializerFactory.GetSerializer <HttpMessageSendChangesResponse>();
                    using var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
                    var getMoreChanges       = await webSerializer.DeserializeAsync(responseStream);

                    context = getMoreChanges.SyncContext;

                    if (getMoreChanges != null && getMoreChanges.Changes != null && getMoreChanges.Changes.HasRows)
                    {
                        var localSerializer = new LocalJsonSerializer();

                        var interceptorsWriting = this.interceptors.GetInterceptors <SerializingRowArgs>();
                        if (interceptorsWriting.Count > 0)
                        {
                            localSerializer.OnWritingRow(async(syncTable, rowArray) =>
                            {
                                var args = new SerializingRowArgs(context, syncTable, rowArray);
                                await this.InterceptAsync(args, progress, cancellationToken).ConfigureAwait(false);
                                return(args.Result);
                            });
                        }


                        // Should have only one table
                        var table       = getMoreChanges.Changes.Tables[0];
                        var schemaTable = DbSyncAdapter.CreateChangesTable(schema.Tables[table.TableName, table.SchemaName]);

                        var fullPath = Path.Combine(serverBatchInfo.GetDirectoryFullPath(), bpi.FileName);

                        // open the file and write table header
                        await localSerializer.OpenFileAsync(fullPath, schemaTable).ConfigureAwait(false);

                        foreach (var row in table.Rows)
                        {
                            await localSerializer.WriteRowToFileAsync(new SyncRow(schemaTable, row), schemaTable).ConfigureAwait(false);
                        }

                        // Close file
                        await localSerializer.CloseFileAsync(fullPath, schemaTable).ConfigureAwait(false);
                    }
                }
                else
                {
                    // Serialize
                    await SerializeAsync(response, bpi.FileName, serverBatchInfo.GetDirectoryFullPath(), this).ConfigureAwait(false);
                }

                // Raise response from server containing a batch changes
                await this.InterceptAsync(new HttpGettingServerChangesResponseArgs(serverBatchInfo, bpi.Index, bpi.RowsCount, summaryResponseContent.SyncContext, this.GetServiceHost()), progress, cancellationToken).ConfigureAwait(false);
            });

            // Parrallel download of all bpis (which will launch the delete directory on the server side)
            await serverBatchInfo.BatchPartsInfo.ForEachAsync(bpi => dl(bpi), this.MaxDownladingDegreeOfParallelism).ConfigureAwait(false);

            // Send order of end of download
            var lastBpi = serverBatchInfo.BatchPartsInfo.FirstOrDefault(bpi => bpi.IsLastBatch);

            if (lastBpi != null)
            {
                var endOfDownloadChanges = new HttpMessageGetMoreChangesRequest(context, lastBpi.Index);

                var serializerEndOfDownloadChanges = this.SerializerFactory.GetSerializer <HttpMessageGetMoreChangesRequest>();
                var binaryData3 = await serializerEndOfDownloadChanges.SerializeAsync(endOfDownloadChanges).ConfigureAwait(false);

                var endResponse = await this.httpRequestHandler.ProcessRequestAsync(
                    this.HttpClient, context, this.ServiceUri, binaryData3, HttpStep.SendEndDownloadChanges,
                    this.SerializerFactory, this.Converter, 0, this.SyncPolicy, cancellationToken, progress).ConfigureAwait(false);

                // Deserialize response incoming from server
                // This is the last response
                // Should contains step HttpStep.SendEndDownloadChanges
                using var streamResponse = await endResponse.Content.ReadAsStreamAsync().ConfigureAwait(false);

                var endResponseSerializer = this.SerializerFactory.GetSerializer <HttpMessageSendChangesResponse>();
                var endResponseContent    = await endResponseSerializer.DeserializeAsync(streamResponse);

                context = endResponseContent.SyncContext;
            }

            // generate the new scope item
            this.CompleteTime = DateTime.UtcNow;

            await this.InterceptAsync(new HttpBatchesDownloadedArgs(summaryResponseContent, summaryResponseContent.SyncContext, this.GetServiceHost()), progress, cancellationToken).ConfigureAwait(false);

            var serverSyncChanges = new ServerSyncChanges(
                summaryResponseContent.RemoteClientTimestamp,
                serverBatchInfo,
                summaryResponseContent.ServerChangesSelected
                );


            return(context, serverSyncChanges, summaryResponseContent.ClientChangesApplied, summaryResponseContent.ConflictResolutionPolicy);
        }
 public ProposedHostHandlerModule(NetworkInterfaceCard communicationModule, ContainerTable containerTable, ILoadManager loadManager) : base(communicationModule, containerTable, loadManager)
 {
     MinUtilization = Global.MinUtilization;
     MaxUtilization = Global.MaxUtilization;
 }
Example #33
0
        protected override bool SendPushRequest()
        {
            BidLock = 0;
            var containerLoadInfo = GetToBeRemovedContainerLoadInfo();

            Console.WriteLine($"I'm Host #{MachineId} and I am Pushing container #{containerLoadInfo} and I have {ContainerTable.GetContainersCount()} contatiners");
            if (containerLoadInfo != null)
            {
                PushRequest m = new PushRequest(0, this.MachineId, LoadManager.GetPredictedHostLoadInfo(), containerLoadInfo);
                CommunicationModule.SendMessage(m);
                return(true);
            }
            else
            {
                return(false);
            }
        }
 public ZhaoHostHandler(NetworkInterfaceCard communicationModule, ContainerTable containerTable, ILoadManager loadManager, CommonLoadManager commonLoadManager) : base(communicationModule, containerTable, loadManager)
 {
     _commonLoadManager = commonLoadManager;
 }