Beispiel #1
0
        private static void ProcessCommands(IList<Tuple<IOverlayNodeService, IOverlayStorageService>> services)
        {
            Func<string, IOverlayNodeService, OverlayData[]> getStoragePutKeyValuesFromCmd = (string arg, IOverlayNodeService srv) => {
                var res = new List<OverlayData>();
                foreach (var pair in arg.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) {
                    var pairAttrVal = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    // assume we have only one overlay level as for now
                    res.Add(new OverlayData {
                        AttrValue = new KeyValuePair<string,object>(pairAttrVal[0].Trim(), pairAttrVal[1].Trim()),
                        OwnerNode = new NodeDescriptor(srv.Node)
                    });
                }
                return res.ToArray();
            };

            Func<string, string[]> getStorageRemoveKeysFromCmd = (string arg) => {
                return arg
                    .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => s.Trim())
                    .ToArray();
            };

            while (true) {
                var cmd = System.Console.ReadLine();

                var operatingSrv = services[Rnd.Next(0, services.Count)];

                if (cmd.StartsWith("join")) {
                    var existingNodesInOverlay = services.Select(s => s.Item1.Node).ToArray();

                    // chord service
                    var nodeWhoJoinsInitInfo = new NodeDescriptor.NodeInitInfo(
                                    NetworkHelper.GetLocalIpAddress(),
                                    OverlayHelper.GetRandomPortNumber(existingNodesInOverlay.Select(n => n.Port).ToArray()),
                                    1
                                );
                    var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");
                    var nodeWhoJoins = new NodeDescriptor(
                                            nodeWhoJoinsInitInfo,
                                            hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nodeWhoJoinsInitInfo))
                                        );
                    var nodeWhoJoinsChordService = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");
                    nodeWhoJoinsChordService.Bind(new LocalNode(nodeWhoJoins));
                    nodeWhoJoinsChordService.Join(
                        existingNodesInOverlay[Rnd.Next(0, existingNodesInOverlay.Length)]
                    );

                    // respective storage service
                    var nodeWhoJoinsStorageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
                    nodeWhoJoinsStorageService.Bind(new[] { nodeWhoJoinsChordService }.ToList());
                    nodeWhoJoinsStorageService.Start();
                } else if (cmd == "leave") {
                    var cmdParts = cmd.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    var srvToLeave = services.FirstOrDefault(s => s.Item1.Node.Id == Convert.ToInt64(cmdParts[1]));
                    if (srvToLeave != null) {
                        srvToLeave.Item2.Stop();
                        srvToLeave.Item1.SendMessage(new OverlayNodeServiceMessage { MsgType = OverlayNodeServiceMessage.MessageType.ExitRequested, WaitForProcessing = true });
                    } else {
                        System.Console.WriteLine("No such node");
                    }
                } else if (cmd == "stop") {
                    break;
                } else if (cmd.StartsWith("stats")) {
                    var cmdParts = cmd.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                    using (var fs = File.Create("generated_stats_" + DateTime.Now.Ticks + ".xml")) {
                        using (var writer = new StreamWriter(fs)) {
                            foreach (var stats in Stats) {
                                writer.WriteLine(String.Join(
                                    ";",
             									stats.NodesCount,
                                    stats.AVPairsCount,
                                    stats.ServiceRegistrationFailsCount,
                                    stats.NodesOverAVThresholdCount,
                                    stats.AvgRegistrationsPerSecond
                                ));
                            }
                        }
                    }
                } else if (cmd.StartsWith("populate")) {
                    var cmdParts = cmd.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    PopulateStorage(
                        services,
                        GetServiceDescriptions(cmdParts[1]),
                        cmdParts.Length > 2 ? (int?)Convert.ToInt32(cmdParts[2]) : null
                    );
                } else if (cmd.StartsWith("put")) {
                    Func<string, IOverlayNodeService, ServiceDescription> f = (string srvDescr, IOverlayNodeService overlaySrv) => {
                        var res = new ServiceDescription {
                            Name = srvDescr.Substring(0, srvDescr.IndexOf(":")),
                            OwnerNode = overlaySrv.Node,
                            Data = new List<OverlayData>()
                        };
                        foreach (var pair in srvDescr.Substring(srvDescr.IndexOf(":") + 1).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) {
                            var pairAttrVal = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                            // assume we have only one overlay level as for now
                            res.Data.Add(
                                new OverlayData {
                                    AttrValue = new KeyValuePair<string,object>(pairAttrVal[0].Trim(), pairAttrVal[1].Trim()),
                                    ServiceName = res.Name,
                                    OwnerNode = res.OwnerNode
                                }
                            );
                        }
                        return res;
                    };
                    var cmdParts = cmd.Substring("put".Length).Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    var ownerSrv = services.FirstOrDefault(s => s.Item1.Node.Id == Convert.ToInt64(cmdParts[1]));
                    if (ownerSrv != null) {
                        ownerSrv.Item2.PutService(f(
                            cmdParts[1],
                            ownerSrv.Item1
                        ));
                    } else {
                        System.Console.WriteLine("No such node");
                    }
                } else if (cmd.StartsWith("remove")) {
                    operatingSrv.Item2.RemoveService(cmd.Substring("remove".Length).Trim());
                } else if (cmd.StartsWith("update")) {
                    var cmdParts = cmd.Substring("update".Length).Split(':', ';');
                    var addCmdPart = cmdParts[1];
                    var removeCmdPart = cmd.IndexOf(";") > 0 ? cmdParts[2] : null;
                    operatingSrv.Item2.UpdateService(
                        cmdParts[0].Trim(),
                        String.IsNullOrWhiteSpace(addCmdPart)
                            ? null
                            : getStoragePutKeyValuesFromCmd(addCmdPart, operatingSrv.Item1),
                        String.IsNullOrWhiteSpace(removeCmdPart)
                            ? null
                            : getStorageRemoveKeysFromCmd(removeCmdPart)
                    );
                } else if (cmd.StartsWith("get")) {
                    var res = operatingSrv.Item2.GetServices(Query.FromRelex(cmd.Substring("get".Length).Trim()), true);
                    if (res != null && res.IsSuccessful && res.ServicesData.Any()) {
                        System.Console.WriteLine("**************************************************");
                        System.Console.WriteLine("RESULTS:");
                        foreach (var r in res.ServicesData) {
                            System.Console.WriteLine("\r\n==============================================================");
                            System.Console.WriteLine(String.Format("Service name: {0}", r.Name));
                            foreach (var d in r.Data) {
                                System.Console.WriteLine(String.Format("{0} = {1} (Responsible node: {2})", d.AttrValue.Key, d.GetFormattedValue(), d.ResponsibleNode.ToString()));
                            }
                            System.Console.WriteLine(String.Format("Service owner: {0}", r.OwnerNode.ToString()));
                            System.Console.WriteLine("==============================================================\r\n");
                        }
                    } else {
                        System.Console.WriteLine(res != null && !res.IsSuccessful ? "Failed" : "No data");
                    }
                }
            }
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            Rnd = new Random();

            Stats = new List<Statistics>();

            // TEMP
            DumpGeneratedServicesToFile = false;

            // log4net init
            NReco.Logging.LogManager.Configure(new NReco.Log4Net.Logger());

            // winter service provider init
            IComponentsConfig config = ConfigurationManager.GetSection("components") as IComponentsConfig;
            SrvPrv = new NReco.Winter.ServiceProvider(config);

            // overlay params
            Params = (NService.Chord.OverlayParams)SrvPrv.GetService("chordOverlayParams");
            Params.FillFromConsoleArgs(args);

            DbMgr = (IDbManager)SrvPrv.GetService("mongoDbManager");

            var serviceGenerationInputs = args[0].Split(new char[] { ',' });

            var serviceDescriptions = GetServiceDescriptions(serviceGenerationInputs[0]);

            // init services
            var p2pServices = new List<Tuple<IOverlayNodeService, IOverlayStorageService>>();

            var registeredNodeIds = new List<long>();

            if (args.Length > 1 && !String.IsNullOrEmpty(args[1])) {
                var overlayNodesCount = Convert.ToInt32(args[1]);
                for (var i = 0; i < overlayNodesCount; ++i) {
                    var chordService = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");

                    var nInit = new NodeDescriptor.NodeInitInfo(
                                        Params.IpAddress,
                                        OverlayHelper.GetRandomPortNumber(),
                                        1
                                    );

                    var nodeId = (chordService as IHashKeyProvider).GetHashKey(OverlayHelper.GetNodeHashArgument(nInit));

                    if (!registeredNodeIds.Any(x => x == nodeId)) {

                        chordService.Bind(new LocalNode(new NodeDescriptor(
                            nInit,
                            nodeId
                        )));

                        // respective storage service
                        var storageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
                        storageService.Bind(new[] { chordService }.ToList());
                        storageService.Start();

                        p2pServices.Add(Tuple.Create(chordService, storageService));

                        registeredNodeIds.Add(nodeId);
                    }
                }
            } else {
                var alreadyProcessedOwnerNodeIds = new Dictionary<int, IOverlayNode>();
                foreach (var srv in serviceDescriptions) {
                    var existingOverlayNode = alreadyProcessedOwnerNodeIds.Where(n => Convert.ToInt64(n.Key) == srv.OwnerNode.Id);
                    if (existingOverlayNode == null || !existingOverlayNode.Any()) {
                        var chordService = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");

                        var nInit = new NodeDescriptor.NodeInitInfo(
                                        Params.IpAddress,
                                        OverlayHelper.GetRandomPortNumber(),
                                        1
                                    );
                        chordService.Bind(new LocalNode(new NodeDescriptor(
                            nInit,
                            srv.OwnerNode.Id
                        )));

                        alreadyProcessedOwnerNodeIds.Add(Convert.ToInt32(srv.OwnerNode.Id), chordService.Node);

                        // set real owner node for the service description
                        srv.OwnerNode = chordService.Node;
                        foreach (var d in srv.Data) {
                            d.OwnerNode = srv.OwnerNode;
                        }

                        // respective storage service
                        var storageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
                        storageService.Bind(new[] { chordService }.ToList());
                        storageService.Start();

                        p2pServices.Add(Tuple.Create(chordService, storageService));
                    } else {
                        // we already created all needed stuff for this node, just set real owner node for respective service description
                        srv.OwnerNode = existingOverlayNode.Single().Value;
                        foreach (var d in srv.Data) {
                            d.OwnerNode = srv.OwnerNode;
                        }
                    }
                }
            }

            // create the overlay
            var alreadyJoinedNodesPorts = new List<int>();
            foreach (var p2p in p2pServices) {
                var overlaySrv = p2p.Item1;

                if (!alreadyJoinedNodesPorts.Any()) {
                    overlaySrv.Join();
                    alreadyJoinedNodesPorts.Add(overlaySrv.Node.Port);
                } else {
                    var nInitLoc = new NodeDescriptor.NodeInitInfo(
                                    Params.IpAddress,
                                    alreadyJoinedNodesPorts[Rnd.Next(0, alreadyJoinedNodesPorts.Count)],
                                    1
                                );
                    var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");
                    overlaySrv.Join(new NodeDescriptor(
                        nInitLoc,
                        hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nInitLoc))
                    ));
                    alreadyJoinedNodesPorts.Add(overlaySrv.Node.Port);
                }
                Thread.Sleep(1000);
            }

            System.Console.WriteLine("Waiting until the overlay stabilizes...");
            Thread.Sleep(10000);

            //populate storage
            PopulateStorage(p2pServices, serviceDescriptions, args.Length > 2 ? (int?)Convert.ToInt32(args[2]) : null);

            // auto-populate more for stats
            if (serviceGenerationInputs.Length > 1) {
                foreach (var srvGen in serviceGenerationInputs) {
                    PopulateStorage(p2pServices, GetServiceDescriptions(srvGen), args.Length > 2 ? (int?)Convert.ToInt32(args[2]) : null);
                }
            }

            System.Console.WriteLine("Ready");
            // listen for commands
            try {
                ProcessCommands(p2pServices);
            } finally {
                foreach (var srv in p2pServices) {
                    srv.Item2.Stop();
                }
                foreach (var srv in p2pServices) {
                    ((ChordOverlayNodeService)srv.Item1).MaintenanceSrv.Stop();
                }
                foreach (var srv in p2pServices) {
                    srv.Item1.SendMessage(new OverlayNodeServiceMessage { MsgType = OverlayNodeServiceMessage.MessageType.ExitRequested, WaitForProcessing = true });
                }
            }
        }
Beispiel #3
0
        private static TestObjects InitNode(ref List<int> alreadyJoinedNodesPorts)
        {
            // let's go
            var srv = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");
            var nInit = new NodeDescriptor.NodeInitInfo(
                            Params.IpAddress,
                            OverlayHelper.GetRandomPortNumber(alreadyJoinedNodesPorts.ToArray()),
                            1
                        );
            srv.Bind(new LocalNode(new NodeDescriptor(
                nInit,
                (srv as IHashKeyProvider).GetHashKey(OverlayHelper.GetNodeHashArgument(nInit))
            )));

            // respective storage service
            var storageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
            storageService.Bind(new[] { srv }.ToList());
            storageService.Start();

            // join the network
            if (!alreadyJoinedNodesPorts.Any()) {
                srv.Join();
                alreadyJoinedNodesPorts.Add(srv.Node.Port);
            } else {
                var nInitLoc = new NodeDescriptor.NodeInitInfo(
                                Params.IpAddress,
                                alreadyJoinedNodesPorts[Rnd.Next(0, alreadyJoinedNodesPorts.Count)],
                                1
                            );
                var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");
                srv.Join(new NodeDescriptor(
                    nInitLoc,
                    hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nInitLoc))
                ));
                alreadyJoinedNodesPorts.Add(srv.Node.Port);
            }

            return new TestObjects {
                StorageSrv = storageService,
                NodeSrv = srv
            };
        }
Beispiel #4
0
        private static TestObjects MainInternal(string[] args, bool listenForCommands, ref List<int> alreadyJoinedNodesPorts, int? bootstrapperNodePort = null, int? localNodePort = null)
        {
            var nodeServices = new List<IOverlayNodeService>();

            // let's go
            for (int currRingLvl = 1; currRingLvl <= Params.OverlayLevels; ++currRingLvl) {
                // node service
                var srv = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");
                var nInit = new NodeDescriptor.NodeInitInfo(
                                Params.IpAddress,
                                localNodePort.HasValue ? localNodePort.Value : (listenForCommands ? Params.Port : OverlayHelper.GetRandomPortNumber()),
                                currRingLvl
                            );
                srv.Bind(new LocalNode(new NodeDescriptor(
                    nInit,
                    (srv as IHashKeyProvider).GetHashKey(OverlayHelper.GetNodeHashArgument(nInit))
                )));
                nodeServices.Add(srv);
            }

            // assume we have only one srv layer
            var oneSrv = nodeServices[0];

            // respective storage service
            var storageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
            storageService.Bind(nodeServices);
            storageService.Start();

            if (listenForCommands) {
                // listen for commands
                try {
                    ProcessCommands(nodeServices, storageService);
                } finally {
                    foreach (var srv in nodeServices) {
                        srv.Stop();
                    }
                }
            } else {
                if (!alreadyJoinedNodesPorts.Any()) {
                    oneSrv.Join();
                    alreadyJoinedNodesPorts.Add(oneSrv.Node.Port);
                } else {
                    var nInit = new NodeDescriptor.NodeInitInfo(
                                    Params.IpAddress,
                                    bootstrapperNodePort.HasValue ? bootstrapperNodePort.Value : alreadyJoinedNodesPorts[Rnd.Next(0, alreadyJoinedNodesPorts.Count)],
                                    1
                                );
                    var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");
                    oneSrv.Join(new NodeDescriptor(
                        nInit,
                        hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nInit))
                    ));
                    alreadyJoinedNodesPorts.Add(oneSrv.Node.Port);
                }
            }

            return new TestObjects {
                StorageSrv = storageService,
                NodeSrv = oneSrv
            };
        }
Beispiel #5
0
        private static void ProcessCommands(IList<IOverlayNodeService> srvs, IOverlayStorageService stSrv)
        {
            CancellationTokenSource showLocalInfoTs = null;
            var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");

            Func<string, OverlayData[]> getStoragePutKeyValuesFromCmd = (string arg) => {
                var res = new List<OverlayData>();
                foreach (var pair in arg.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) {
                    var pairAttrVal = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    // assume we have only one overlay level as for now
                    res.Add(new OverlayData {
                        AttrValue = new KeyValuePair<string,object>(pairAttrVal[0].Trim(), pairAttrVal[1].Trim()),
                        OwnerNode = new NodeDescriptor(srvs[0].Node)
                    });
                }
                return res.ToArray();
            };

            Func<string, string[]> getStorageRemoveKeysFromCmd = (string arg) => {
                return arg
                    .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => s.Trim())
                    .ToArray();
            };

            while (true) {
                var cmd = System.Console.ReadLine();
                if (cmd.StartsWith("join")) {
                    var cmdParts = cmd.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    var taskFactory = new TaskFactory(TaskCreationOptions.LongRunning | TaskCreationOptions.AttachedToParent, TaskContinuationOptions.None);
                    if (cmdParts.Length > 1) {
                        var nodeInfo = cmdParts[1].Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var srv in srvs) {
                            // TODO: it is possible to wrap each join in a different thread and then wait on all threads so all joins will perform simultaneously
                            var nInit = new NodeDescriptor.NodeInitInfo(
                                    cmdParts[1].IndexOf(":") > 0 ? nodeInfo[0] : NetworkHelper.GetLocalIpAddress(), // you could specify only the port number
                                    cmdParts[1].IndexOf(":") > 0 ? Convert.ToInt32(nodeInfo[1]) : Convert.ToInt32(nodeInfo[0]),
                                    ((LocalNode)srv.Node).OverlayLevel
             								);
                            srv.Join(new NodeDescriptor(
                                nInit,
                                hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nInit))
                            ));
                        }
                    } else {
                        foreach (var srv in srvs) {
                            // TODO: it is possible to wrap each join in a different thread and then wait on all threads so all joins will perform simultaneously
                            srv.Join();
                        }
                    }
                } else if (cmd == "info") {
                    ShowLocalInfo(srvs);
                } else if (cmd == "startinfo") {
                    showLocalInfoTs = StartShowLocalInfo(srvs);
                } else if (cmd == "stopinfo") {
                    StopShowLocalInfo(showLocalInfoTs);
                } else if (cmd.StartsWith("put")) {
                    Func<string, ServiceDescription> f = (string srvDescr) => {
                        var res = new ServiceDescription {
                            Name = srvDescr.Substring(0, srvDescr.IndexOf(":")),
                            OwnerNode = srvs[0].Node,
                            Data = new List<OverlayData>()
                        };
                        foreach (var pair in srvDescr.Substring(srvDescr.IndexOf(":") + 1).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) {
                            var pairAttrVal = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                            // assume we have only one overlay level as for now
                            res.Data.Add(
                                new OverlayData {
                                    AttrValue = new KeyValuePair<string,object>(pairAttrVal[0].Trim(), pairAttrVal[1].Trim()),
                                    ServiceName = res.Name,
                                    OwnerNode = res.OwnerNode
                                }
                            );
                        }
                        return res;
                    };
                    stSrv.PutService(f(cmd.Substring("put".Length).Trim()));
                } else if (cmd.StartsWith("remove")) {
                    stSrv.RemoveService(cmd.Substring("remove".Length).Trim());
                } else if (cmd.StartsWith("update")) {
                    var cmdParts = cmd.Substring("update".Length).Split(':', ';');
                    var addCmdPart = cmdParts[1];
                    var removeCmdPart = cmd.IndexOf(";") > 0 ? cmdParts[2] : null;
                    stSrv.UpdateService(
                        cmdParts[0].Trim(),
                        String.IsNullOrWhiteSpace(addCmdPart)
                            ? null
                            : getStoragePutKeyValuesFromCmd(addCmdPart),
                        String.IsNullOrWhiteSpace(removeCmdPart)
                            ? null
                            : getStorageRemoveKeysFromCmd(removeCmdPart)
                    );
                } else if (cmd.StartsWith("get")) {
                    var res = stSrv.GetServices(Query.FromRelex(cmd.Substring("get".Length).Trim()), true);
                    if (res != null && res.Any()) {
                        System.Console.WriteLine("RESULTS:");
                        foreach (var r in res) {
                            System.Console.WriteLine(String.Format("Service name: {0}", r.Name));
                            foreach (var d in r.Data) {
                                System.Console.WriteLine(String.Format("{0} = {1} (Responsible node: {2})", d.AttrValue.Key, d.AttrValue.Value, d.ResponsibleNode.ToString()));
                            }
                            System.Console.WriteLine(String.Format("Service owner: {0}", r.OwnerNode.ToString()));
                        }
                    } else {
                        System.Console.WriteLine("No data");
                    }
                } else if (cmd == "leave") {
                    StopShowLocalInfo(showLocalInfoTs);

                    foreach (var srv in srvs) {
                        // wait until all instance internal threads are done
                        srv.SendMessage(new OverlayNodeServiceMessage { MsgType = OverlayNodeServiceMessage.MessageType.LeaveRequested, WaitForProcessing = true });
                    }
                } else if (cmd == "exit") {
                    StopShowLocalInfo(showLocalInfoTs);

                    stSrv.Stop();

                    foreach (var srv in srvs) {
                        // wait until all instance internal threads are done
                        srv.SendMessage(new OverlayNodeServiceMessage { MsgType = OverlayNodeServiceMessage.MessageType.ExitRequested, WaitForProcessing = true });
                    }
                    break;
                } else {
                    System.Console.WriteLine(@"
            join [<address>:]<port> : Join the network with known node \r\n
            join: Create new network \r\n
            info: Show info about local node \r\n
            puts <servicename>:<attr>=<value>[,<attr>=<value>...]: Put service in the overlay \r\n
            put <attr>=<value>[,<attr>=<value>...]: Put data in the overlay \r\n
            removes <servicename>: Remove service data from overlay \r\n
            remove <attr>[,<attr>...]: Remove data associated with given attribute(s) \r\n
            update <servicename>:<add_attr>=<value>[,<add_attr>=<value>...];<remove_attr>[,<remove_attr>...]: Update info about service \r\n
            gets <relex query>: Find services by query \r\n
            get <relex query>: Find data by query \r\n
            leave: Leave the network \r\n
            exit: Exit the application"
                    );
                }
            }
        }
Beispiel #6
0
        public static void Main(string[] args)
        {
            Rnd = new Random();

            // log4net init
            NReco.Logging.LogManager.Configure(new NReco.Log4Net.Logger());

            // winter service provider init
            IComponentsConfig config = ConfigurationManager.GetSection("components") as IComponentsConfig;
            SrvPrv = new NReco.Winter.ServiceProvider(config);

            // overlay params
            Params = (NService.Chord.OverlayParams)SrvPrv.GetService("chordOverlayParams");
            Params.FillFromConsoleArgs(args);

            // prepare overlay storage
            DbMgr = (IDbManager)SrvPrv.GetService("mongoDbManager");
            DbMgr.Clear();

            // init CloudSim
             			CloudSim.init(1, JavaCalendar.getInstance(), true);

            // cloud datacenter
            CloudDatacenter = (NService.Cloud.ServiceDatacenter)SrvPrv.GetService("serviceDatacenter");

            // cloud broker
            CloudBroker = (NService.Cloud.ServiceBroker)SrvPrv.GetService("serviceBroker");
            var brokerVmList = new List<Vm>();

            // create cloud and the overlay based on service descriptions
            var p2pServices = new List<Tuple<IOverlayNodeService, IOverlayStorageService>>();
            var serviceDescriptions = GetServiceDescriptions(args[0]);
            foreach (var service in serviceDescriptions) {
                var chordService = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");

                var nInit = new NodeDescriptor.NodeInitInfo(
                                Params.IpAddress,
                                OverlayHelper.GetRandomPortNumber(null, new Random(Convert.ToInt32(service.OwnerNode.Id))),
                                1
                            );
                var nodeId = (chordService as IHashKeyProvider).GetHashKey(OverlayHelper.GetNodeHashArgument(nInit));

                // create a VM for the node
                var nodeVm = new Vm(
                        Convert.ToInt32(nodeId),
                        CloudBroker.getId(),
                        400,
                        1,
                        512,
                        1000,
                        10000,
                        "Xen",
                        new CloudletSchedulerTimeShared()
                    );
                brokerVmList.Add(nodeVm);

                chordService.Bind(new LocalNode(new VmOverlayNode(
                    nInit,
                    nodeId,
                    nodeVm
                )));

                // set real owner node for the service description
                service.OwnerNode = chordService.Node;
                foreach (var d in service.Data) {
                    d.OwnerNode = service.OwnerNode;
                }

                // respective storage service
                var storageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
                storageService.Bind(new[] { chordService }.ToList());
                storageService.Start();

                p2pServices.Add(Tuple.Create(chordService, storageService));
            }

            // submit all VMs to the broker
            CloudBroker.submitVmList(JavaUtils.ToJavaList(brokerVmList));

            // create the overlay
            var alreadyJoinedNodesPorts = new List<int>();
            foreach (var p2p in p2pServices) {
                var overlaySrv = p2p.Item1;

                if (!alreadyJoinedNodesPorts.Any()) {
                    overlaySrv.Join();
                    alreadyJoinedNodesPorts.Add(overlaySrv.Node.Port);
                } else {
                    var nInitLoc = new NodeDescriptor.NodeInitInfo(
                                    Params.IpAddress,
                                    alreadyJoinedNodesPorts[Rnd.Next(0, alreadyJoinedNodesPorts.Count)],
                                    1
                                );
                    var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");
                    overlaySrv.Join(new NodeDescriptor(
                        nInitLoc,
                        hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nInitLoc))
                    ));
                    alreadyJoinedNodesPorts.Add(overlaySrv.Node.Port);
                }
            }

            System.Console.WriteLine("Waiting until the overlay stabilizes...");
            Thread.Sleep(10000);

            System.Console.WriteLine("Populating the data storage...");
            //populate storage
            foreach (var srv in serviceDescriptions) {
                var ownerNodeStorageSrv = p2pServices.Single(s => s.Item2.Node.Equals(srv.OwnerNode)).Item2;
                ownerNodeStorageSrv.PutService(srv);
            }

            CloudSim.startSimulation();

            System.Console.WriteLine("Ready");
            // listen for commands
            try {
                ProcessCommands(p2pServices);
            } finally {
                foreach (var srv in p2pServices) {
                    srv.Item2.Stop();
                }
                foreach (var srv in p2pServices) {
                    ((ChordOverlayNodeService)srv.Item1).MaintenanceSrv.Stop();
                }
                foreach (var srv in p2pServices) {
                    srv.Item1.SendMessage(new OverlayNodeServiceMessage { MsgType = OverlayNodeServiceMessage.MessageType.ExitRequested, WaitForProcessing = true });
                }
                CloudSim.stopSimulation();
            }
        }
Beispiel #7
0
        private static void ProcessCommands(IList<Tuple<IOverlayNodeService, IOverlayStorageService>> services)
        {
            Func<string, IOverlayNodeService, OverlayData[]> getStoragePutKeyValuesFromCmd = (string arg, IOverlayNodeService srv) => {
                var res = new List<OverlayData>();
                foreach (var pair in arg.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) {
                    var pairAttrVal = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                    // assume we have only one overlay level as for now
                    res.Add(new OverlayData {
                        AttrValue = new KeyValuePair<string,object>(pairAttrVal[0].Trim(), pairAttrVal[1].Trim()),
                        OwnerNode = new NodeDescriptor(srv.Node)
                    });
                }
                return res.ToArray();
            };

            Func<string, string[]> getStorageRemoveKeysFromCmd = (string arg) => {
                return arg
                    .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(s => s.Trim())
                    .ToArray();
            };

            while (true) {
                var cmd = System.Console.ReadLine();

                var operatingSrv = services[Rnd.Next(0, services.Count)];

                if (cmd.StartsWith("join")) {
                    var existingNodesInOverlay = services.Select(s => s.Item1.Node).ToArray();

                    // chord service
                    var nodeWhoJoinsInitInfo = new NodeDescriptor.NodeInitInfo(
                                    NetworkHelper.GetLocalIpAddress(),
                                    OverlayHelper.GetRandomPortNumber(existingNodesInOverlay.Select(n => n.Port).ToArray()),
                                    1
                                );
                    var hashPrv = (IProvider<byte[], long>)SrvPrv.GetService("sha1KeyPrv");
                    var nodeWhoJoinsId = hashPrv.Provide(OverlayHelper.GetNodeHashArgument(nodeWhoJoinsInitInfo));
                    var nodeWhoJoinsVm = new Vm(
                            Convert.ToInt32(nodeWhoJoinsId),
                            CloudBroker.getId(),
                            400,
                            1,
                            512,
                            1000,
                            10000,
                            "Xen",
                            new CloudletSchedulerTimeShared()
                        );
                    var nodeWhoJoins = new VmOverlayNode(
                                            nodeWhoJoinsInitInfo,
                                            nodeWhoJoinsId,
                                            nodeWhoJoinsVm
                                        );
                    var nodeWhoJoinsChordService = (IOverlayNodeService)SrvPrv.GetService("chordOverlayNodeService");
                    nodeWhoJoinsChordService.Bind(new LocalNode(nodeWhoJoins));
                    nodeWhoJoinsChordService.Join(
                        existingNodesInOverlay[Rnd.Next(0, existingNodesInOverlay.Length)]
                    );

                    // respective storage service
                    var nodeWhoJoinsStorageService = (IOverlayStorageService)SrvPrv.GetService("mongoDbOverlayStorageService");
                    nodeWhoJoinsStorageService.Bind(new[] { nodeWhoJoinsChordService }.ToList());
                    nodeWhoJoinsStorageService.Start();

                    // add a VM to the broker
                    CloudBroker.scheduleNow(
                        CloudDatacenter.getName(),
                        CloudSimTags.VM_CREATE,
                        nodeWhoJoinsVm
                    );

                } else if (cmd == "leave") {
                    var cmdParts = cmd.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    var srvToLeave = services.FirstOrDefault(s => s.Item1.Node.Id == Convert.ToInt64(cmdParts[1]));
                    if (srvToLeave != null) {
                        srvToLeave.Item2.Stop();
                        srvToLeave.Item1.SendMessage(new OverlayNodeServiceMessage { MsgType = OverlayNodeServiceMessage.MessageType.ExitRequested, WaitForProcessing = true });

                        // remove a VM for this node
                        CloudBroker.scheduleNow(
                            CloudDatacenter.getName(),
                            CloudSimTags.VM_DESTROY,
                            ((VmOverlayNode)srvToLeave.Item1.Node).OwnedVm
                        );
                    } else {
                        System.Console.WriteLine("No such node");
                    }
                } else if (cmd == "stop") {
                    break;
                } else if (cmd.StartsWith("put")) {
                    Func<string, IOverlayNodeService, ServiceDescription> f = (string srvDescr, IOverlayNodeService overlaySrv) => {
                        var res = new ServiceDescription {
                            Name = srvDescr.Substring(0, srvDescr.IndexOf(":")),
                            OwnerNode = overlaySrv.Node,
                            Data = new List<OverlayData>()
                        };
                        foreach (var pair in srvDescr.Substring(srvDescr.IndexOf(":") + 1).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) {
                            var pairAttrVal = pair.Split(new string[] { "=" }, StringSplitOptions.RemoveEmptyEntries);
                            // assume we have only one overlay level as for now
                            res.Data.Add(
                                new OverlayData {
                                    AttrValue = new KeyValuePair<string,object>(pairAttrVal[0].Trim(), pairAttrVal[1].Trim()),
                                    ServiceName = res.Name,
                                    OwnerNode = res.OwnerNode
                                }
                            );
                        }
                        return res;
                    };
                    var cmdParts = cmd.Substring("put".Length).Trim().Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    var ownerSrv = services.FirstOrDefault(s => s.Item1.Node.Id == Convert.ToInt64(cmdParts[1]));
                    if (ownerSrv != null) {
                        ownerSrv.Item2.PutService(f(
                            cmdParts[1],
                            ownerSrv.Item1
                        ));
                    } else {
                        System.Console.WriteLine("No such node");
                    }
                } else if (cmd.StartsWith("remove")) {
                    operatingSrv.Item2.RemoveService(cmd.Substring("remove".Length).Trim());
                } else if (cmd.StartsWith("update")) {
                    var cmdParts = cmd.Substring("update".Length).Split(':', ';');
                    var addCmdPart = cmdParts[1];
                    var removeCmdPart = cmd.IndexOf(";") > 0 ? cmdParts[2] : null;
                    operatingSrv.Item2.UpdateService(
                        cmdParts[0].Trim(),
                        String.IsNullOrWhiteSpace(addCmdPart)
                            ? null
                            : getStoragePutKeyValuesFromCmd(addCmdPart, operatingSrv.Item1),
                        String.IsNullOrWhiteSpace(removeCmdPart)
                            ? null
                            : getStorageRemoveKeysFromCmd(removeCmdPart)
                    );
                } else if (cmd.StartsWith("get")) {
                    var res = operatingSrv.Item2.GetServices(Query.FromRelex(cmd.Substring("get".Length).Trim()), true);
                    if (res != null && res.Any()) {
                        System.Console.WriteLine("**************************************************");
                        System.Console.WriteLine("RESULTS:");
                        foreach (var r in res) {
                            System.Console.WriteLine("\r\n==============================================================");
                            System.Console.WriteLine(String.Format("Service name: {0}", r.Name));
                            foreach (var d in r.Data) {
                                System.Console.WriteLine(String.Format("{0} = {1} (Responsible node: {2})", d.AttrValue.Key, d.GetFormattedValue(), d.ResponsibleNode.ToString()));
                            }
                            System.Console.WriteLine(String.Format("Service owner: {0}", r.OwnerNode.ToString()));
                            System.Console.WriteLine("==============================================================\r\n");
                        }
                    } else {
                        System.Console.WriteLine("No data");
                    }
                }
            }
        }