public void Stop()
        {
            if (!m_started)
            {
                return;
            }
            try
            {
                Log.WriteLine(LogLevel.Debug, "Stopping communication instance.");

                //  TODO notify the modules
                if (HasHttpEndpoints())
                {
                    StopHttpServer();
                }

                //  Unregister cloud storage
                memory_cloud = null;

                //  Shutdown message passing networking
                NativeNetwork.StopTrinityServer();

                //  Unregister communication instance
                Global.CommunicationInstance = null;
                var _config = TrinityConfig.CurrentClusterConfig;
                m_started = false;
                Log.WriteLine("{0} {1} is successfully stopped.", RunningMode, _config.MyInstanceId);
            }
            catch (Exception ex)
            {
                Log.WriteLine(LogLevel.Error, "CommunicationInstance: " + ex.ToString());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Stops a Trinity instance.
        /// </summary>
        public void Stop()
        {
            lock (m_lock)
            {
                if (!m_started)
                {
                    return;
                }
                try
                {
                    int id = memory_cloud.MyInstanceId;
                    Log.WriteLine(LogLevel.Debug, "Stopping communication instance.");

                    //  TODO notify the modules
                    StopCommunicationListeners();

                    m_module_init_signal.Reset();

                    //  Unregister cloud storage
                    memory_cloud = null;

                    //  Unregister communication instance
                    Global.CommunicationInstance = null;

                    m_started = false;
                    Log.WriteLine("{0} {1} is successfully stopped.", RunningMode, id);
                }
                catch (Exception ex)
                {
                    Log.WriteLine(LogLevel.Error, "CommunicationInstance: " + ex.ToString());
                }
            }
        }
Beispiel #3
0
 public ClientIStorage(MemoryCloud mc)
 {
     m_messages   = new ConcurrentQueue <TrinityMessage>();
     m_req_tokens = new ConcurrentDictionary <long, TaskCompletionSource <bool> >();
     m_rsp_tokens = new ConcurrentDictionary <long, TaskCompletionSource <TrinityResponse> >();
     m_mc         = mc;
 }
Beispiel #4
0
        /// <summary>
        /// It is guaranteed that CloudStorage can be accessed (server started)
        /// before a client calls module ClientInitialize() method.
        /// </summary>
        public unsafe void ClientInitialize(RunningMode remoteRunningMode, MemoryCloud mc)
        {
            m_memorycloud = mc;
            string moduleName          = GetModuleName();
            IMessagePassingEndpoint rs = remoteRunningMode == RunningMode.Server ?
                                         mc.StorageTable.FirstOrDefault(_ => !(_ is LocalMemoryStorage)) as IMessagePassingEndpoint :
                                         mc.ProxyList.FirstOrDefault();

            if (null == rs)
            {
                string msg = "ClientInitialize: " + moduleName + ": No remote communication instance found.";
                Log.WriteLine(LogLevel.Error, msg);
                throw new InvalidOperationException(msg);
            }

            ushort synReqOffset;
            ushort synReqRspOffset;
            ushort asynReqOffset;
            ushort asynReqRspOffset;

            if (!rs.GetCommunicationModuleOffset(moduleName, out synReqOffset, out synReqRspOffset, out asynReqOffset, out asynReqRspOffset))
            {
                string msg = "CommunicationModule " + moduleName + " not found on the remote side.";
                Log.WriteLine(LogLevel.Error, msg);
                throw new InvalidOperationException(msg);
            }
            else
            {
                this.SynReqIdOffset     = synReqOffset;
                this.SynReqRspIdOffset  = synReqRspOffset;
                this.AsynReqIdOffset    = asynReqOffset;
                this.AsynReqRspIdOffset = asynReqRspOffset;
            }
        }
Beispiel #5
0
 /// <summary>
 /// Creates a cloud storage instance with the specified name.
 /// </summary>
 /// <param name="config">A ClusterConfig instance.</param>
 /// <returns>The newly created cloud storage instance.</returns>
 public static MemoryCloud CreateCloudStorage(ClusterConfig config)
 {
     lock (s_storage_init_lock)
     {
         MemoryCloud mc = new MemoryCloud(config);
         mc.RegisterGenericOperationsProvider(generic_cell_ops);
         s_registered_memoryclouds.Add(mc);
         return(mc);
     }
 }
Beispiel #6
0
        /// <summary>
        /// Starts a Trinity instance.
        /// </summary>
        public void Start()
        {
            lock (m_lock)
            {
                if (m_started)
                {
                    return;
                }
                try
                {
                    Log.WriteLine(LogLevel.Debug, "Starting communication instance.");
                    var _config = TrinityConfig.CurrentClusterConfig;

                    _config.RunningMode          = this.RunningMode;
                    Global.CommunicationInstance = this;

                    if (_InstanceList.Count == 0)
                    {
                        Log.WriteLine(LogLevel.Warning, "No distributed instances configured. Turning on local test mode.");
                        TrinityConfig.LocalTest = true;
                    }

                    //  Initialize message handlers
                    MessageHandlers.Initialize();
                    RegisterMessageHandler();

                    //  Initialize message passing networking
                    NativeNetwork.StartTrinityServer((UInt16)_config.ListeningPort);
                    Log.WriteLine("My IPEndPoint: " + _config.MyBoundIP + ":" + _config.ListeningPort);

                    //  Initialize cloud storage
                    memory_cloud = Global.CloudStorage;

                    //  Initialize the modules
                    _InitializeModules();

                    if (HasHttpEndpoints())
                    {
                        StartHttpServer();
                    }

                    Console.WriteLine("Working Directory: {0}", Global.MyAssemblyPath);
                    Console.WriteLine(_config.OutputCurrentConfig());
                    Console.WriteLine(TrinityConfig.OutputCurrentConfig());

                    m_started = true;
                    Log.WriteLine("{0} {1} is successfully started.", RunningMode, _config.MyInstanceId);
                    _RaiseStartedEvents();
                }
                catch (Exception ex)
                {
                    Log.WriteLine(LogLevel.Error, "CommunicationInstance: " + ex.ToString());
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Constructs a MessageSorter instance
        /// </summary>
        public MessageSorter(MemoryCloud mc = null)
        {
            memory_cloud = mc;
            if (memory_cloud == null)
            {
                memory_cloud = Global.CloudStorage;
            }

            dest_cell_ids = new List <long> [memory_cloud.ServerCount];
            for (int i = 0; i < memory_cloud.ServerCount; i++)
            {
                dest_cell_ids[i] = new List <long>();
            }
        }
Beispiel #8
0
 public CloudIndex(CancellationToken token, INameService namesvc, IChunkTable ctable, MemoryCloud mc, string nickname, Func <int, Partition> ptable)
 {
     m_mc           = mc;
     m_myname       = nickname;
     m_partitions   = ptable;
     m_nameservice  = namesvc;
     m_chunktable   = ctable;
     m_cancel       = token;
     m_storagetab   = new Dictionary <Guid, IStorage>();
     m_ctcache      = new Dictionary <Guid, IEnumerable <Chunk> >();
     m_masters      = new IStorage[m_nameservice.PartitionCount];
     m_replicaList  = Utils.Integers(m_nameservice.PartitionCount).Select(_ => Enumerable.Empty <ReplicaInformation>()).ToArray();
     m_ctupdateproc = Utils.Daemon(m_cancel, "ChunkTableUpdateProc", 10000, ChunkTableUpdateProc);
     m_masterproc   = Utils.Daemon(m_cancel, "MasterNotifyProc", 10000, MasterNotifyProc);
     m_scanproc     = Utils.Daemon(m_cancel, "ScanNodesProc", 10000, ScanNodesProc);
 }
Beispiel #9
0
        /// <summary>
        /// Starts a Trinity instance.
        /// </summary>
        public unsafe void Start()
        {
            lock (m_lock)
            {
                if (m_started)
                {
                    return;
                }
                try
                {
                    Log.WriteLine(LogLevel.Debug, "Starting communication instance.");
                    Global.CommunicationInstance     = this;
                    TrinityConfig.CurrentRunningMode = this.RunningMode;

                    //  Initialize message handlers
                    MessageHandlers.Initialize();
                    RegisterMessageHandler();
                    MessageDispatcher = _MessageInitializationTrap;

                    //  Bring up networking subsystems
                    StartCommunicationListeners();

                    //  Initialize cloud storage
                    memory_cloud = Global.CloudStorage;

                    //  Initialize the modules
                    _ScanForAutoRegisteredModules();
                    _InitializeModules();

                    //  Modules initialized, release pending messages from the trap
                    m_module_init_signal.Set();
                    MessageDispatcher = MessageHandlers.DefaultParser.DispatchMessage;

                    Log.WriteLine("Working Directory: {0}", Global.MyAssemblyPath);
                    Log.WriteLines(TrinityConfig.OutputCurrentConfig());

                    m_started = true;
                    Log.WriteLine("{0} {1} is successfully started.", RunningMode, memory_cloud.MyInstanceId);
                    _RaiseStartedEvents();
                }
                catch (Exception ex)
                {
                    Log.WriteLine(LogLevel.Error, "CommunicationInstance: " + ex.ToString());
                }
            }
        }
        /// <summary>
        /// It is guaranteed that CloudStorage can be accessed (server started)
        /// before a client calls module ClientInitialize() method.
        /// </summary>
        internal protected unsafe void ClientInitialize(RunningMode remoteRunningMode, MemoryCloud mc = null)
        {
            //Debug.Assert(TrinityConfig.CurrentRunningMode == RunningMode.Client);
            //Debug.Assert(remoteRunningMode == RunningMode.Server || remoteRunningMode == RunningMode.Proxy);

            if (mc == null)
            {
                mc = Global.CloudStorage;
            }
            m_memoryCloud = mc;

            string        moduleName = GetModuleName();
            RemoteStorage rs         = remoteRunningMode == RunningMode.Server ?
                                       mc.StorageTable.FirstOrDefault(_ => _ is RemoteStorage) as RemoteStorage :
                                       mc.ProxyList.FirstOrDefault();

            if (null == rs)
            {
                string msg = "ClientInitialize: " + moduleName + ": No remote communication instance found.";
                Log.WriteLine(LogLevel.Error, msg);
                throw new InvalidOperationException(msg);
            }

            ushort synReqOffset;
            ushort synReqRspOffset;
            ushort asynReqOffset;

            if (!rs.GetCommunicationModuleOffset(moduleName, out synReqOffset, out synReqRspOffset, out asynReqOffset))
            {
                string msg = "CommunicationModule " + moduleName + " not found on the remote side.";
                Log.WriteLine(LogLevel.Error, msg);
                throw new InvalidOperationException(msg);
            }
            else
            {
                this.SynReqIdOffset    = synReqOffset;
                this.SynReqRspIdOffset = synReqRspOffset;
                this.AsynReqIdOffset   = asynReqOffset;
            }

            SetupMessagePassingInterfaces(remoteRunningMode);
        }
Beispiel #11
0
        private unsafe void ServerInitialize(CommunicationInstance instance)
        {
            m_memorycloud = Global.CloudStorage;
            ICommunicationSchema schema = this.GetCommunicationSchema();

            this.SynReqIdOffset     = instance.SynReqIdOffset;
            this.SynReqRspIdOffset  = instance.SynReqRspIdOffset;
            this.AsynReqIdOffset    = instance.AsynReqIdOffset;
            this.AsynReqRspIdOffset = instance.AsynReqRspIdOffset;

            checked
            {
                instance.SynReqIdOffset     += (ushort)schema.SynReqProtocolDescriptors.Count();
                instance.SynReqRspIdOffset  += (ushort)schema.SynReqRspProtocolDescriptors.Count();
                instance.AsynReqIdOffset    += (ushort)schema.AsynReqProtocolDescriptors.Count();
                instance.AsynReqRspIdOffset += (ushort)schema.AsynReqRspProtocolDescriptors.Count();
                // each ASYNC_WITH_RSP message comes with a response handler.
                instance.AsynReqIdOffset += (ushort)schema.AsynReqRspProtocolDescriptors.Count();
            }
        }
        /// <summary>
        /// It is guaranteed that CloudStorage can be accessed (native server started)
        /// before an instance calls module Initialize() method.
        /// </summary>
        internal unsafe void Initialize(CommunicationInstance instance)
        {
            //Debug.Assert(TrinityConfig.CurrentRunningMode != RunningMode.Client);
            //Debug.Assert(TrinityConfig.CurrentRunningMode != RunningMode.Embedded);
            //Debug.Assert(TrinityConfig.CurrentRunningMode != RunningMode.Undefined);

            ICommunicationSchema schema = this.GetCommunicationSchema();

            m_memoryCloud = instance.CloudStorage;

            this.SynReqIdOffset    = instance.SynReqIdOffset;
            this.SynReqRspIdOffset = instance.SynReqRspIdOffset;
            this.AsynReqIdOffset   = instance.AsynReqIdOffset;

            checked
            {
                instance.SynReqIdOffset    += (ushort)schema.SynReqProtocolDescriptors.Count();
                instance.SynReqRspIdOffset += (ushort)schema.SynReqRspProtocolDescriptors.Count();
                instance.AsynReqIdOffset   += (ushort)schema.AsynReqProtocolDescriptors.Count();
            }

            this.RegisterMessageHandler();
            SetupMessagePassingInterfaces(instance.RunningMode);
        }
Beispiel #13
0
 public DynamicRemoteStorage(ReplicaInformation server_info, int connPerServer, MemoryCloud mc)
     : base(new[] { new ServerInfo(server_info.Address, server_info.Port, null, LogLevel.Info) }, connPerServer, mc, server_info.PartitionId, nonblocking: true)
 {
     ReplicaInformation = server_info;
     NickName           = Utils.GenerateNickName(ReplicaInformation.Id);
 }