コード例 #1
0
        void _BeforeLoad(byte[] dlldata, string classname, string pluginsource)
        {
            ensureopen("BeforeLoad");
            ensurenotsortd("BeforeLoad");
            ensurenotenumd("BeforeLoad");

            foreach (SlaveInfo slave in dslaves)
            {
                slave.nstm.WriteByte((byte)'F'); // 'F' for BeforeLoad (first)
                if (null == classname)
                {
                    XContent.SendXContent(slave.nstm, "");
                }
                else
                {
                    XContent.SendXContent(slave.nstm, classname);
                }
                XContent.SendXContent(slave.nstm, pluginsource);
                XContent.SendXContent(slave.nstm, dlldata);
            }

            // "Join"...
            foreach (SlaveInfo slave in dslaves)
            {
                slave.SlaveErrorCheck();
            }
        }
コード例 #2
0
ファイル: Remote.cs プロジェクト: xwyangjshb/qizmt
        void _RemoteExec(byte[] dlldata, string classname, string pluginsource)
        {
            foreach (SlaveInfo _slave in dslaves)
            {
                BufSlaveInfo slave = (BufSlaveInfo)_slave;
                slave.nstm.WriteByte((byte)'R'); // 'R' for remoteexec
                if (null == classname)
                {
                    XContent.SendXContent(slave.nstm, "");
                }
                else
                {
                    XContent.SendXContent(slave.nstm, classname);
                }
                XContent.SendXContent(slave.nstm, pluginsource);
                XContent.SendXContent(slave.nstm, dlldata);
            }

            // "Join"...
            foreach (SlaveInfo slave in dslaves)
            {
                slave.nstm.WriteByte((byte)'.'); // Ping.
                if ((int)',' != slave.nstm.ReadByte())
                {
                    throw new Exception("RemoteExec sync error (pong)");
                }
            }
        }
コード例 #3
0
ファイル: Remote.cs プロジェクト: xwyangjshb/qizmt
        // Note: appendsizes can be null, but the return will still be valid.
        // Sizes exclude header data.
        protected virtual int GetNumberOfRemoteOutputFilesCreated(int n, IList <long> appendsizes)
        {
            int result = 0;

            foreach (SlaveInfo slave in dslaves)
            {
                slave.nstm.WriteByte((byte)'r');
                Entry.ToBytes(n, buf, 0);
                XContent.SendXContent(slave.nstm, buf, 4);
                int len;
                buf = XContent.ReceiveXBytes(slave.nstm, out len, buf);
                if (len >= 4)
                {
                    result += Entry.BytesToInt(buf);

                    if (null != appendsizes)
                    {
                        for (int offset = 4; offset + 8 <= len; offset += 8)
                        {
                            appendsizes.Add(Entry.BytesToLong(buf, offset));
                        }
                    }
                }
            }
            return(result);
        }
コード例 #4
0
ファイル: Hashtable.cs プロジェクト: xwyangjshb/qizmt
        internal bool cangetnext()
        {
            lock (slave)
            {
                if (first)
                {
                    first = false;
                    internalsendreset();
                }

                // 'N' for next..
                slave.nstm.WriteByte((byte)'N');
                slave.nstm.WriteByte(enumid);

                // Replies with tag '+' and xcontent key and xcontent value; or tag '-' if not exists.
                int len;
                int x = slave.nstm.ReadByte();
                if ('+' == x)
                {
                    byte[] key   = XContent.ReceiveXBytes(slave.nstm, out len, null); // Null buffer for copy.
                    byte[] value = XContent.ReceiveXBytes(slave.nstm, out len, null); // Null buffer for copy.
                    cur = new System.Collections.DictionaryEntry(key, value);
                    return(true);
                }
                else if ('-' == x)
                {
                    return(false);
                }
                else
                {
                    throw new Exception("Server returned invalid response for enumeration");
                }
            }
        }
コード例 #5
0
ファイル: Remote.cs プロジェクト: xwyangjshb/qizmt
 protected virtual void GetDGlobals()
 {
     foreach (SlaveInfo slave in dslaves)
     {
         slave.nstm.WriteByte((byte)'O');
         int len = 0;
         buf = XContent.ReceiveXBytes(slave.nstm, out len, buf);
         DGlobalsM.FromBytes(buf, len, true);
     }
 }
コード例 #6
0
ファイル: LongIntComboList.cs プロジェクト: xwyangjshb/qizmt
 internal void FlushAddBuf_unlocked()
 {
     if (addbuflen > 0)
     {
         int xlen = addbuflen;
         addbuflen = 0;
         nstm.WriteByte((byte)'p'); // Put/publish batch.
         XContent.SendXContent(nstm, addbuf, xlen);
     }
 }
コード例 #7
0
        public void SendXmlConfig(string xml)
        {
            foreach (SlaveInfo slave in dslaves)
            {
                slave.nstm.WriteByte((byte)'<');
                XContent.SendXContent(slave.nstm, xml);
            }

            PlusValidate("SendXmlConfig");
        }
コード例 #8
0
 // Returns true if flushed, false if nothing to flush.
 internal bool FlushAddBuf_unlocked()
 {
     if (addbuflen > 0)
     {
         int xlen = addbuflen;
         addbuflen = 0;
         nstm.WriteByte((byte)'p'); // Put/publish batch.
         XContent.SendXContent(nstm, addbuf, xlen);
         return(true);
     }
     return(false);
 }
コード例 #9
0
            protected string GetLastErrorReset()
            {
#if SLAVE_GET_LAST_ERROR
                nstm.WriteByte((byte)'?');
                int len;
                errbuf = XContent.ReceiveXBytes(nstm, out len, errbuf);
                if (0 != len)
                {
                    return(Encoding.UTF8.GetString(errbuf, 0, len));
                }
#endif
                return(null); // No error.
            }
コード例 #10
0
ファイル: LongIntComboList.cs プロジェクト: xwyangjshb/qizmt
        public override void Open()
        {
            base.Open();

            if (mindupes > 0)
            {
                foreach (SlaveInfo slave in dslaves)
                {
                    slave.nstm.WriteByte((byte)'M');
                    DistObject.ToBytes(mindupes, buf, 0);
                    XContent.SendXContent(slave.nstm, buf, 4);
                }
            }
        }
コード例 #11
0
        public static string GetNetworkPath(System.Net.Sockets.NetworkStream nstm, string host)
        {
#if VARIABLE_NETWORK_PATHS
            nstm.WriteByte((byte)'d'); // Get current directory.

            if ((int)'+' != nstm.ReadByte())
            {
                throw new Exception("GetNetworkPath failure (service didn't report success)");
            }

            return(LocalPathToNetworkPath(XContent.ReceiveXString(nstm, null), host));
#else
            return(LocalPathToNetworkPath(_netpath, host));
#endif
        }
コード例 #12
0
        public virtual void Add(int a, int b)
        {
            if (!didopen)
            {
                throw new Exception("Must Open before writing to Hashtable");
            }

            B8 b8; // = new B8(a, b);

            b8.A = a;
            b8.B = b;

            //b8.CopyToArray(buf);
            //int slaveID = DetermineSlave(buf, 8);
            int slaveID = ((b8.A ^ b8.B) & 0x7FFFFFFF) % dslaves.Count;

            /*if (slaveID < 0 || slaveID >= dslaves.Count)
             * {
             *  throw new Exception("Slave missing: slaveID needed: " + slaveID.ToString());
             * }*/
            SlaveInfo slave = dslaves[slaveID];

            lock (slave)
            {
                if (addbufinitsize > 0)
                {
                    BufSlaveInfo bslave = (BufSlaveInfo)slave;
                    bslave.EnsureAddBuf_unlocked(addbufinitsize);
                    b8.CopyToArray(buf);
                    bslave.AddBuf_unlocked(buf, 8);
                }
                else
                {
                    // 'P' for publish/put
                    slave.nstm.WriteByte((byte)'P');
                    DistObject.ToBytes(Int32.MaxValue, buf, 0);
                    XContent.SendXContent(slave.nstm, buf, 4);
                    b8.CopyToArray(buf);
                    XContent.SendXContent(slave.nstm, buf, 8);
                    // Note: no confirmation
                }
            }
        }
コード例 #13
0
 internal void internalsendreset()
 {
     if (null != dlldata)
     {
         // 'D' to send DLL containing IBeforeReduceFixed plugin..
         slave.nstm.WriteByte((byte)'D');
         if (null == classname)
         {
             XContent.SendXContent(slave.nstm, "");
         }
         else
         {
             XContent.SendXContent(slave.nstm, classname);
         }
         XContent.SendXContent(slave.nstm, this.plugincode);
         XContent.SendXContent(slave.nstm, dlldata);
         slave.SlaveErrorCheck();
     }
 }
コード例 #14
0
        public int nextbufend = 0; // Where in nextbuf the valid elements end.

        internal bool cangetnext()
        {
            lock (slave)
            {
                if (first)
                {
                    first = false;
                    internalsendreset();
                }

                if (nextbufpos < nextbufend)
                {
                    fillcurfromnextbuf();
                    return(true);
                }
                else
                {
                    // 'e for batch next..
                    slave.nstm.WriteByte((byte)'e');

                    // Replies with tag '+' and xcontent values; or tag '-' if not exists.
                    //int len;
                    int x = slave.nstm.ReadByte();
                    if ('+' == x)
                    {
                        nextbufpos = 0;
                        nextbufend = 0;
                        nextbuf    = XContent.ReceiveXBytes(slave.nstm, out nextbufend, nextbuf);
                        slave.SlaveErrorCheck();
                        fillcurfromnextbuf();
                        return(true);
                    }
                    else if ('-' == x)
                    {
                        return(false);
                    }
                    else
                    {
                        throw new Exception("Server returned invalid response for enumeration");
                    }
                }
            }
        }
コード例 #15
0
ファイル: Hashtable.cs プロジェクト: xwyangjshb/qizmt
        public override void Open()
        {
            base.Open();

            byte[] bappendbuffersize = null;
            if (appendbuffersize >= 0)
            {
                bappendbuffersize = ToBytes(appendbuffersize);

                foreach (SlaveInfo slave in dslaves)
                {
                    //if (appendbuffersize >= 0)
                    {
                        slave.nstm.WriteByte((byte)'b'); // EnableAppendBuffer
                        XContent.SendXContent(slave.nstm, bappendbuffersize);
                    }
                }
            }
        }
コード例 #16
0
ファイル: Hashtable.cs プロジェクト: xwyangjshb/qizmt
        // Returns -1 if no such key.
        public int Length(byte[] key)
        {
            if (!didopen)
            {
                throw new Exception("Must Open before writing to Hashtable");
            }

            int slaveID = DetermineSlave(key);

            if (slaveID < 0 || slaveID >= dslaves.Count)
            {
                throw new Exception("Sub process missing: subProcessID needed: " + slaveID.ToString());
            }
            SlaveInfo slave = dslaves[slaveID];

            lock (slave)
            {
                // 'l' for get length
                slave.nstm.WriteByte((byte)'l');
                XContent.SendXContent(slave.nstm, key);

                // Replies with tag '+' and xcontent length; or tag '-' if not exists.
                byte[] result = null;
                int    len;
                int    x = slave.nstm.ReadByte();
                if ('+' == x)
                {
                    result = XContent.ReceiveXBytes(slave.nstm, out len, buf);
                    return(DistObjectBase.BytesToInt(result)); // Only reads first one.
                }
                else if ('-' == x)
                {
                    //result = null;
                    return(-1);
                }
                else
                {
                    throw new Exception("Server returned invalid response for l (Length)");
                }
            }
        }
コード例 #17
0
ファイル: VitalsReporter.cs プロジェクト: xwyangjshb/qizmt
        void TattleThreadProc()
        {
            string        localhost = System.Net.Dns.GetHostName();
            List <string> badhosts  = new List <string>(10);

            while (!stop)
            {
                long newlastupdated = RogueHosts.Get(jobid, tattlelastupdated, badhosts);
                if (newlastupdated != tattlelastupdated) // has new tattled host
                {
                    tattlelastupdated = newlastupdated;
                    if (badhosts.Count > 0)
                    {
                        string strbhs = "";
                        for (int i = 0; i < badhosts.Count; i++)
                        {
                            if (i > 0)
                            {
                                strbhs += ";";
                            }
                            strbhs += badhosts[i];

                            if (string.Compare(badhosts[i], localhost, true) == 0)
                            {
                                badlocalhost = true;
                            }
                        }
                        lock (clientstm)
                        {
                            clientstm.WriteByte((byte)'e');
                            XContent.SendXContent(clientstm, strbhs);
                        }
#if FAILOVER_DEBUG
                        System.IO.File.AppendAllText(@"c:\temp\vitalsreporter_A5B1E053-9A32-417b-8068-91A7CD5CDEAB.txt", DateTime.Now.ToString() + " rogues sent:" + strbhs + Environment.NewLine);
#endif
                    }
                }
                System.Threading.Thread.Sleep(tattletimeout);
            }
        }
コード例 #18
0
ファイル: Hashtable.cs プロジェクト: xwyangjshb/qizmt
        // Gives null if no such key.
        public byte[] Get(byte[] key)
        {
            if (!didopen)
            {
                throw new Exception("Must Open before reading from Hashtable");
            }

            int slaveID = DetermineSlave(key);

            if (slaveID < 0 || slaveID >= dslaves.Count)
            {
                throw new Exception("Sub process missing: subProcessID needed: " + slaveID.ToString());
            }
            SlaveInfo slave = dslaves[slaveID];

            lock (slave)
            {
                // 'G' for get
                slave.nstm.WriteByte((byte)'G');
                XContent.SendXContent(slave.nstm, key);

                // Replies with tag '+' and xcontent value; or tag '-' if not exists.
                byte[] result = null;
                int    len;
                int    x = slave.nstm.ReadByte();
                if ('+' == x)
                {
                    result = XContent.ReceiveXBytes(slave.nstm, out len, null); // Null buffer for copy.
                }
                else if ('-' == x)
                {
                    //result = null;
                }
                else
                {
                    throw new Exception("Server returned invalid response for G (get)");
                }
                return(result);
            }
        }
コード例 #19
0
ファイル: Hashtable.cs プロジェクト: xwyangjshb/qizmt
        public void Add(byte[] key, byte[] value)
        {
            if (!didopen)
            {
                throw new Exception("Must Open before writing to Hashtable");
            }

            int slaveID = DetermineSlave(key);

            if (slaveID < 0 || slaveID >= dslaves.Count)
            {
                throw new Exception("Sub process missing: subProcessID needed: " + slaveID.ToString());
            }
            SlaveInfo slave = dslaves[slaveID];

            lock (slave)
            {
                // 'P' for publish/put
                slave.nstm.WriteByte((byte)'P');
                XContent.SendXContent(slave.nstm, key);
                XContent.SendXContent(slave.nstm, value);
                // Note: no confirmation
            }
        }
コード例 #20
0
ファイル: Hashtable.cs プロジェクト: xwyangjshb/qizmt
        public void Length(byte[] key, int setlength)
        {
            if (!didopen)
            {
                throw new Exception("Must Open before writing to Hashtable");
            }

            int slaveID = DetermineSlave(key);

            if (slaveID < 0 || slaveID >= dslaves.Count)
            {
                throw new Exception("Sub process missing: subProcessID needed: " + slaveID.ToString());
            }
            SlaveInfo slave = dslaves[slaveID];

            lock (slave)
            {
                // 'L' for set length
                slave.nstm.WriteByte((byte)'L');
                XContent.SendXContent(slave.nstm, key);
                XContent.SendXContent(slave.nstm, DistObjectBase.ToBytes(setlength));
                // Note: no confirmation
            }
        }
コード例 #21
0
        protected override void ProcessCommand(NetworkStream nstm, char tag)
        {
            //string s;
            int len;

            switch (tag)
            {
            case 'N':     // Next in enumeration..
            {
                int ienumid = nstm.ReadByte();
                if (ienumid >= 0)
                {
                    byte enumid = (byte)ienumid;
                    if (enumid >= this.enums.Length)
                    {
                        nstm.WriteByte((byte)'-');
                    }
                    else
                    {
                        if (null == this.enums[enumid])
                        {
                            this.enums[enumid] = table.GetEnumerator();
                        }
                        if (this.enums[enumid].MoveNext())
                        {
                            nstm.WriteByte((byte)'+');
                            System.Collections.DictionaryEntry de = this.enums[enumid].Entry;
                            XContent.SendXContent(nstm, (byte[])de.Key);
                            if (AppendBufferingEnabled)
                            {
                                AppendBuffer abvalue = (AppendBuffer)de.Value;
                                byte[]       result  = abvalue.Get(out len);
                                XContent.SendXContent(nstm, result, len);
                            }
                            else
                            {
                                XContent.SendXContent(nstm, (byte[])de.Value);
                            }
                        }
                        else
                        {
                            nstm.WriteByte((byte)'-');
                        }
                    }
                }
            }
            break;

            case 'n':     // Reset next in enumeration..
            {
                int ienumid = nstm.ReadByte();
                if (ienumid >= 0)
                {
                    byte enumid = (byte)ienumid;
                    if (XLog.logging)
                    {
                        XLog.log("Starting enumeration (enumid:" + enumid.ToString() + ")");
                    }
                    if (enumid < this.enums.Length &&
                        null != this.enums[enumid])
                    {
                        //this.enums[enumid].Reset();
                        this.enums[enumid] = null;
                    }
                }
            }
            break;

            case 'b':     // Enable AppendBuffer
                XContent.ReceiveXBytes(nstm, out len, buf);
                appendbuffersize = BytesToInt(buf);
                if (appendbuffersize < 0 || appendbuffersize >= 16777216)
                {
                    throw new Exception("Invalid AppendBuffer preallocation size (appendbuffersize): " + appendbuffersize.ToString());
                }
                if (XLog.logging)
                {
                    XLog.log("Enabled AppendBuffer with initial value sizes of " + appendbuffersize.ToString());
                }
                break;

            case 'L':     // Set length..
            {
                // Key..
                byte[] putkey = XContent.ReceiveXBytes(nstm, out len, buf);
                putkey = GetSliceCopy(putkey, len);         // !
                // New length..
                byte[] putlength = XContent.ReceiveXBytes(nstm, out len, buf);
                int    newlength = BytesToInt(putlength);

                if (AppendBufferingEnabled)
                {
                    object ovalue;
                    if (UseStringKeys)
                    {
                        ovalue = table[Encoding.ASCII.GetString(putkey)];
                    }
                    else
                    {
                        ovalue = table[putkey];
                    }
                    if (null == ovalue)
                    {
                        int setbufsize = newlength;
                        if (setbufsize < appendbuffersize)
                        {
                            setbufsize = appendbuffersize;         // ?
                        }
                        if (UseStringKeys)
                        {
                            table[Encoding.ASCII.GetString(putkey)] = new AppendBuffer(setbufsize);
                        }
                        else
                        {
                            table[putkey] = new AppendBuffer(setbufsize);
                        }
                    }
                    else
                    {
                        AppendBuffer abvalue = (AppendBuffer)ovalue;
                        abvalue.SetLength(newlength);
                    }
                }
                else
                {
                    byte[] oldvalue = GetValue(putkey, out len);
                    if (newlength < len)
                    {
                        CopyAndSetValue(putkey, oldvalue, newlength);
                    }
                }
            }
            break;

            case 'G':     // Get..
            {
                //Thread.Sleep(15000);

                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                byte[] getkey = GetSliceCopy(buf, len);

                byte[] result = GetValue(getkey, out len);
                if (null == result)
                {
                    nstm.WriteByte((byte)'-');         // Key doesn't exist!
                }
                else
                {
                    nstm.WriteByte((byte)'+');         // It exists!
                    XContent.SendXContent(nstm, result, len);
                }
            }
            break;

            case 'l':     // Get length..
            {
                byte[] getkey = XContent.ReceiveXBytes(nstm, out len, buf);
                getkey = GetSliceCopy(getkey, len);

                byte[] value = GetValue(getkey, out len);
                if (null == value)
                {
                    nstm.WriteByte((byte)'-');         // Key doesn't exist!
                }
                else
                {
                    nstm.WriteByte((byte)'+');         // It exists!
                    XContent.SendXContent(nstm, ToBytes(len));
                }
            }
            break;

            case 'P':     // Put..
            {
                // Key..
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                byte[] putkey = GetSliceCopy(buf, len);
                // Value..
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                CopyAndSetValue(putkey, buf, len);
            }
            break;

            case 'a':     // Math add..
            {
                // Key..
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                byte[] putkey = GetSliceCopy(buf, len);         // !
                // Operand..
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                if (buf.Length >= 4)
                {
                    int operand = BytesToInt(buf);
                    // Do add...
                    byte[] val = GetValue(putkey, out len);
                    if (null == val || val.Length < 4)         // Key doesn't exist, or too small, so assume it was 0.
                    {
                        ToBytes(operand, buf, 0);
                        CopyAndSetValue(putkey, buf, 4);
                    }
                    else
                    {
                        int existingint = BytesToInt(val);
                        int result      = existingint + operand;
                        //XLog.log("blarg", "adding " + existingint.ToString() + " and " + operand.ToString() + ", got result " + result.ToString());
                        ToBytes(result, buf, 0);
                        CopyAndSetValue(putkey, buf, 4);
                    }
                }
            }
            break;

            case 'A':     // Append..
            {
                // Key..
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                byte[] putkey = GetSliceCopy(buf, len);
                // Value..
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                AppendValue(putkey, buf, len);
            }
            break;

            default:
                base.ProcessCommand(nstm, tag);
                break;
            }
        }
コード例 #22
0
ファイル: RemoteSlave.cs プロジェクト: xwyangjshb/qizmt
        protected override void ProcessCommand(System.Net.Sockets.NetworkStream nstm, char tag)
        {
            int len;

            switch (tag)
            {
            case 'R':     // Remote!
            {
                string classname = XContent.ReceiveXString(nstm, buf);

                string xlibfn = CreateXlibFileName("remote");
                {
                    buf = XContent.ReceiveXBytes(nstm, out len, buf);
                    if (0 != len)
                    {
                        System.IO.FileStream stm = System.IO.File.Create(xlibfn);
                        stm.Write(buf, 0, len);
                        stm.Close();
                    }
                }

                string dllfn = CreateDllFileName("remote");
                {
                    buf = XContent.ReceiveXBytes(nstm, out len, buf);
                    System.IO.FileStream stm = System.IO.File.Create(dllfn);
                    stm.Write(buf, 0, len);
                    stm.Close();
                }

                if (XLog.logging)
                {
                    string xclassname = classname;
                    if (null == xclassname)
                    {
                        xclassname = "<null>";
                    }
                    XLog.log("Loading IRemote plugin named " + xclassname + " for remote: " + dllfn);
                }

                rem = LoadRemotePlugin(dllfn, classname);
#if DEBUG
                try
                {
                    rem.OnRemote();
                }
                catch (Exception e)
                {
                    throw new UserException(e);
                }
#else
                rem.OnRemote();
#endif
            }
            break;

            case 'O':     //Query DGlobals
            {
                int byteCount = DGlobalsM.ToBytes(ref buf);
                XContent.SendXContent(nstm, buf, byteCount);
            }
            break;

            case 'r':
            {
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                int n     = Entry.BytesToInt(buf);
                int count = 0;
                if (null != rem)
                {
                    List <long> appendsizes = new List <long>();
                    try
                    {
                        count = rem.GetOutputFileCount(n, appendsizes);
                    }
                    catch (Exception e)
                    {
                        throw new DistributedObjectsSlave.DistObjectAbortException(e);
                    }

                    if (buf.Length < 4 + 8 * appendsizes.Count)
                    {
                        buf = new byte[Entry.Round2Power(4 + 8 * appendsizes.Count)];
                    }
                    Entry.ToBytes(count, buf, 0);
                    int offset = 4;
                    for (int i = 0; i < appendsizes.Count; i++, offset += 8)
                    {
                        Entry.LongToBytes(appendsizes[i], buf, offset);
                    }
                    XContent.SendXContent(nstm, buf, 4 + 8 * appendsizes.Count);
                    break;         // !
                }
                Entry.ToBytes(count, buf, 0);
                XContent.SendXContent(nstm, buf, 4);
            }
            break;

            default:
                base.ProcessCommand(nstm, tag);
                break;
            }
        }
コード例 #23
0
        public virtual void ProcessCommands(NetworkStream nstm)
        {
            //nstm = new XNetworkStream(sock);
            int x;

            this.nstm = nstm;
            try
            {
                for (; ;)
                {
                    x = nstm.ReadByte();
                    if (x < 0)
                    {
                        //throw new Exception("Unable to read tag for command");
                        break;
                    }
                    buf[0]         = (byte)x;
                    CurrentCommand = (char)x;

                    if ('\\' == (char)buf[0])
                    {
                        if (XLog.logging)
                        {
                            XLog.log("Received close command from service; closing");
                        }
                        break;
                    }
                    else if ('<' == (char)buf[0])
                    {
                        try
                        {
                            string xml = XContent.ReceiveXString(nstm, buf);
                            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
                            xd.LoadXml(xml);
                            DistributedObjectsSlave.InitXmlConfig(xd);
                            ReloadConfig();
                        }
                        catch
                        {
                            nstm.WriteByte((byte)'-');
                            throw;
                        }
                        nstm.WriteByte((byte)'+');
                        continue;
                    }
                    else if ('?' == (char)buf[0])
                    {
                        if (null != lasterror)
                        {
                            string lerr = lasterror;
                            lasterror = null; // Also resets error!
                            XContent.SendXContent(nstm, lerr);
                        }
                        else
                        {
                            XContent.SendXContent(nstm, buf, 0); // No error!
                        }
                        continue;
                    }

                    /*
                     * if (XLog.logging)
                     * {
                     *  XLog.log("Slave processing command " + ((char)buf[0]).ToString());
                     * }
                     * */
                    ProcessCommand(nstm, (char)buf[0]);
                }
            }
            catch (System.IO.IOException ioex)
            {
                // Drop SocketException (shutdown during tasks), rethrow others.
                if ((ioex.InnerException as SocketException) == null)
                {
                    throw;
                }
#if DEBUG
                XLog.errorlog("DistributedObjectsSlave Warning: IOException+SocketException during task shutdown: " + ioex.ToString());
#endif
            }

            nstm.Close();
            nstm.Dispose();
        }
コード例 #24
0
        // args: <ipaddr> <portnum> <typechar> <capacity> <logfile> <jid>
        static void Main(string[] args)
        {
            MySpace.DataMining.AELight.Surrogate.LogonMachines();

            try
            {
#if DEBUGnoisy
                XLog.errorlog("DistributedObjectsSlave EntryPoint: " + Environment.CommandLine);
#endif

#if DEBUG
                //System.Threading.Thread.Sleep(1000 * 8);
                {
                    //string computer_name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
                    //if (computer_name == "MAPDDRULE" || computer_name == "MAPDCMILLER")
                    {
                        if (System.IO.File.Exists("sleep.txt"))
                        {
                            System.Threading.Thread.Sleep(1000 * 8);
                            int i32 = 1 + 32;
                        }
                    }
                }
#endif

#if DEBUG
                SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
#endif

                XLog.UserLogFile = args[4];
                if (XLog.UserLogFile.StartsWith("&"))
                {
                    XLog.UserLogFile       = XLog.UserLogFile.Substring(1);
                    XLog.DelimitUserErrors = true;
                }

#if SLAVE_TRACE
                try
                {
                    Thread mainthread             = System.Threading.Thread.CurrentThread;
                    System.Threading.Thread stthd = new System.Threading.Thread(
                        new System.Threading.ThreadStart(
                            delegate
                    {
                        string spid = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
                        try
                        {
                            string dotracefile          = spid + ".trace";
                            const string tracefiledelim = "{C8683F6C-0655-42e7-ACD9-0DDED6509A7C}";
                            for (; ;)
                            {
                                System.IO.StreamWriter traceout = null;
                                for (System.Threading.Thread.Sleep(1000 * 60)
                                     ; !System.IO.File.Exists(dotracefile)
                                     ; System.Threading.Thread.Sleep(1000 * 60))
                                {
                                }
                                {
                                    string[] tfc;
                                    try
                                    {
                                        tfc = System.IO.File.ReadAllLines(dotracefile);
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                    if (tfc.Length < 1 || "." != tfc[tfc.Length - 1])
                                    {
                                        continue;
                                    }
                                    try
                                    {
                                        System.IO.File.Delete(dotracefile);
                                    }
                                    catch
                                    {
                                        continue;
                                    }
                                    if ("." != tfc[0])
                                    {
                                        string traceoutfp = tfc[0];
                                        try
                                        {
                                            traceout = System.IO.File.CreateText(traceoutfp);
                                            traceout.Write("BEGIN:");
                                            traceout.WriteLine(tracefiledelim);
                                        }
                                        catch
                                        {
                                            continue;
                                        }
                                    }
                                }
                                if (null == traceout)
                                {
                                    XLog.log("SLAVE_TRACE: " + spid + " Start");
                                }
                                for (; ; System.Threading.Thread.Sleep(1000 * 60))
                                {
                                    {
                                        try
                                        {
#if SLAVE_TRACE_PORT
                                            if (null == traceout)
                                            {
                                                try
                                                {
                                                    StringBuilder sbtp = new StringBuilder();
                                                    sbtp.Append("SLAVE_TRACE_PORT: " + spid + ":");
                                                    for (int i = 0; i < DOSlave_TracePorts.Count; i++)
                                                    {
                                                        sbtp.Append(' ');
                                                        sbtp.Append(DOSlave_TracePorts[i]);
                                                    }
                                                    if (0 == DOSlave_TracePorts.Count)
                                                    {
                                                        sbtp.Append(" None");
                                                    }
                                                    XLog.log(sbtp.ToString());
                                                }
                                                catch
                                                {
                                                }
                                            }
#endif
                                            bool thdsuspended = false;
                                            try
                                            {
                                                mainthread.Suspend();
                                                thdsuspended = true;
                                            }
                                            catch (System.Threading.ThreadStateException)
                                            {
                                            }
                                            try
                                            {
                                                System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(mainthread, false);
                                                StringBuilder sbst       = new StringBuilder();
                                                const int maxframesprint = 15;
                                                for (int i = 0, imax = Math.Min(maxframesprint, st.FrameCount); i < imax; i++)
                                                {
                                                    if (0 != sbst.Length)
                                                    {
                                                        sbst.Append(", ");
                                                    }
                                                    string mn = "N/A";
                                                    try
                                                    {
                                                        System.Reflection.MethodBase mb = st.GetFrame(i).GetMethod();
                                                        mn = mb.ReflectedType.Name + "." + mb.Name;
                                                    }
                                                    catch
                                                    {
                                                    }
                                                    sbst.Append(mn);
                                                }
                                                if (st.FrameCount > maxframesprint)
                                                {
                                                    sbst.Append(" ... ");
                                                    sbst.Append(st.FrameCount - maxframesprint);
                                                    sbst.Append(" more");
                                                }
                                                if (null == traceout)
                                                {
                                                    XLog.log("SLAVE_TRACE: " + spid + " Trace: " + sbst.ToString());
                                                }
                                                else
                                                {
                                                    traceout.WriteLine(sbst.ToString());
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                XLog.log("SLAVE_TRACE: " + spid + " Error: " + e.ToString());
                                            }
                                            finally
                                            {
                                                if (thdsuspended)
                                                {
                                                    mainthread.Resume();
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            XLog.log("SLAVE_TRACE: " + spid + " " + mainthread.Name + " Trace Error: Cannot access thread: " + e.ToString());
                                        }
                                    }

                                    if (null != traceout)
                                    {
                                        traceout.Write(tracefiledelim);
                                        traceout.WriteLine(":END");
                                        traceout.Close();
                                        break;
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            XLog.log("SLAVE_TRACE: " + spid + " Trace Failure: " + e.Message);
                        }
                    }));
                    stthd.IsBackground = true;
                    stthd.Start();
                }
                catch (Exception est)
                {
                    XLog.log("SLAVE_TRACE: Thread start error: " + est.ToString());
                }
#endif

                sjid = args[5];
                {
                    int ic = sjid.IndexOf(':');
                    if (-1 != ic)
                    {
                        jobdesc = Encoding.UTF8.GetString(Convert.FromBase64String(sjid.Substring(ic + 1)));
                        sjid    = sjid.Substring(0, ic);
                    }
                }
                jid = long.Parse(sjid);

                try
                {
                    Environment.SetEnvironmentVariable("DOSLAVE", "DO5");
                }
                catch
                {
                }

                System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
#if DEBUGslaveconfigload
                Random _scsr = new Random(DateTime.Now.Millisecond / 2 + System.Threading.Thread.CurrentThread.ManagedThreadId / 2);
                //for (int i = 0; i < 50; i++)
                {
                    System.Threading.Thread.Sleep(_scsr.Next(50, 200));
                    try
                    {
                        xd.Load("slaveconfig.j" + sjid + ".xml");
                        InitXmlConfig(xd);
                    }
                    catch (System.IO.FileNotFoundException e)
                    {
                        //System.Diagnostics.Debugger.Launch();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debugger.Launch();
                        throw;
                    }
                }
#else
                try
                {
                    xd.Load("slaveconfig.j" + sjid + ".xml");
                    InitXmlConfig(xd);
                }
                catch (System.IO.FileNotFoundException e)
                {
                }
#endif

                if (XLog.logging)
                {
                    XLog.log("New Sub Process: '" + args[0] + "' '" + args[1] + "' '" + args[2] + "' '" + args[3] + "' '" + args[4] + "' '" + args[5] + "'");
                }

                Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                string host    = args[0];
                int    portnum = ushort.Parse(args[1]);
#if SLAVE_TRACE_PORT
                DOSlave_TracePorts.Add(portnum);
#endif
                char typechar = args[2][0];
                try
                {
                    Thread.Sleep(100); // Give DLL a chance to start accepting.
                    sock.Connect(host, portnum);
                }
                catch (Exception e)
                {
                    throw new Exception("Sub-process-to-client(dll) connection error: " + e.ToString() + "  [Note: ensure port " + portnum.ToString() + " is free / not blocked]");
                }
                IDistObject dop;
                if ('H' == typechar)
                {
                    int capacity = ParseCapacity(args[3]);
                    if (XLog.logging)
                    {
                        XLog.log("Connected; creating Hashtable with capacity " + args[3]);
                    }
                    dop = HashtableObjectPart.Create(capacity);
                }
                else if ('8' == typechar)
                {
                    int capacity = ParseCapacity(args[3]);
                    if (XLog.logging)
                    {
                        XLog.log("Connected; creating IntComboList with capacity " + args[3]);
                    }
                    dop = IntComboListPart.Create(capacity);
                }
                else if ('C' == typechar) // 12 hex
                {
                    int capacity = ParseCapacity(args[3]);
                    if (XLog.logging)
                    {
                        XLog.log("Connected; creating LongIntComboList with capacity " + args[3]);
                    }
                    dop = LongIntComboListPart.Create(capacity);
                }
                else if ('A' == typechar)
                {
                    //int capacity = ParseCapacity(args[3]);
                    string[] a = args[3].Split(';');
                    int      count_capacity         = ParseCapacity(a[0]);
                    int      estimated_row_capacity = ParseCapacity(a[1]);
                    int      keylen = int.Parse(a[2]);
                    if (XLog.logging)
                    {
                        XLog.log("Connected; creating ArrayComboList(keylength=" + keylen.ToString() + ") with capacity " + args[3]);
                    }
                    dop = ArrayComboListPart.Create(count_capacity, estimated_row_capacity, keylen);
                }
                else if ('R' == typechar)
                {
                    if (XLog.logging)
                    {
                        XLog.log("Connected; creating Remote");
                    }
                    dop = RemotePart.Create();
                }
                else if ('F' == typechar)
                {
                    //int capacity = ParseCapacity(args[3]);
                    string[] a        = args[3].Split(';');
                    int      keylen   = ParseCapacity(a[2]);
                    int      valuelen = ParseCapacity(a[3]);
                    if (XLog.logging)
                    {
                        XLog.log("Connected; creating FixedArrayComboList(keylength=" + keylen.ToString() + ";valuelength=" + valuelen.ToString() + ")");
                    }
                    dop = FixedArrayComboListPart.Create(keylen, valuelen);
                }
                else
                {
                    throw new Exception("Data type not supported");
                }

                NetworkStream nstm = new XNetworkStream(sock);
                {
                    // New handshake with client(dll)...
                    nstm.WriteByte(1);
                    // ... and the SlavePID:
                    int    SlavePID = System.Diagnostics.Process.GetCurrentProcess().Id;
                    byte[] bpid     = DistObjectBase.ToBytes(SlavePID);
                    XContent.SendXContent(nstm, bpid);
                }

                {
                    int          pid         = System.Diagnostics.Process.GetCurrentProcess().Id;
                    string       spid        = pid.ToString();
                    string       pidfilename = spid + ".j" + sjid + ".slave.pid";
                    StreamWriter pidfile     = new StreamWriter(pidfilename);
                    pidfile.WriteLine(pid);
                    pidfile.WriteLine(System.DateTime.Now);
                    pidfile.WriteLine("jid={0}", sjid);
                    pidfile.Flush();
                    for (; ;)
                    {
                        try
                        {
                            dop.ProcessCommands(nstm);
                            break;
                        }
                        catch (DistObjectAbortException e)
                        {
                            XLog.errorlog("ProcessCommands exception: " + e.ToString() + " ((DistObjectAbortException))");
                            break; // !
                        }
                        catch (Exception e)
                        {
                            XLog.errorlog("ProcessCommands exception: " + e.ToString() + " ((recovering from this exception))");
                        }
                    }
                    pidfile.Close();
                    try
                    {
                        System.IO.File.Delete(pidfilename);
                    }
                    catch
                    {
                    }
                }
            }
            catch (Exception e)
            {
                XLog.errorlog("Main exception: " + e.ToString());
            }
        }
コード例 #25
0
        protected override void ProcessCommand(NetworkStream nstm, char tag)
        {
            //string s;
            int len;

            switch (tag)
            {
            case 'e':     // Batch 'get next' enumeration.
            {
                try
                {
                    int ienumid = nstm.ReadByte();
                    if (ienumid >= 0)
                    {
                        byte enumid = (byte)ienumid;
                        if (enumid >= this.enums.Length)
                        {
                            nstm.WriteByte((byte)'-');
                        }
                        else
                        {
                            if (null == this.enums[enumid])
                            {
                                this.enums[enumid] = new LongIntComboListPartEnumerator(this);
                            }
                            int offset = 0;
                            if (null == buf || buf.Length < 60)
                            {
                                throw new Exception("Enumeration batch buffer too small!");
                            }
                            //if (uniquecompression) // Compressed...
                            {
                                for (; ;)
                                {
                                    if (!this.enums[enumid].MoveNext())
                                    {
                                        break;
                                    }
                                    B12 b12 = this.enums[enumid].Current;
                                    // Using Big Endian!
                                    b12.CopyToArray(buf, offset);
                                    offset += 12;
                                    if (offset + 12 > buf.Length)
                                    {
                                        break;
                                    }
                                }
                            }
                            if (offset > 0)
                            {
                                nstm.WriteByte((byte)'+');
                                XContent.SendXContent(nstm, buf, offset);
                            }
                            else
                            {
                                nstm.WriteByte((byte)'-');
                            }
                        }
                    }
                }
                catch
                {
                    nstm.WriteByte((byte)'-');
                    throw;
                }
            }
            break;

            case 'n':     // Reset next in enumeration..
            {
                int ienumid = nstm.ReadByte();
                if (ienumid >= 0)
                {
                    byte enumid = (byte)ienumid;
                    if (XLog.logging)
                    {
                        XLog.log("Starting enumeration (enumid:" + enumid.ToString() + ")");
                    }
                    if (enumid < this.enums.Length &&
                        null != this.enums[enumid])
                    {
                        //this.enums[enumid].Reset();
                        this.enums[enumid] = null;
                    }
                }
            }
            break;

            case 's':
            {
                try
                {
#if ENABLE_TIMING
                    long start = 0;
                    if (XLog.logging)
                    {
                        QueryPerformanceCounter(out start);
                    }
#endif

                    int readbuflen = 1048576;
                    if (null != DistributedObjectsSlave.xslave)
                    {
                        System.Xml.XmlNode xzblocks = DistributedObjectsSlave.xslave["zblocks"];
                        if (null != xzblocks)
                        {
                            {
                                System.Xml.XmlAttribute xzbs = xzblocks.Attributes["readbuffersize"];
                                if (null != xzbs)
                                {
                                    readbuflen = DistributedObjectsSlave.ParseCapacity(xzbs.Value);
                                }
                            }
                        }
                    }

                    foreach (ZBlock zb in zblocks)
                    {
                        zb.LeaveAddMode(readbuflen);
                    }

                    foreach (ZBlock zb in zblocks)
                    {
                        zb.Sort(b12buffer, this.buf);
                    }

#if ENABLE_TIMING
                    if (XLog.logging)
                    {
                        long stop;
                        QueryPerformanceCounter(out stop);
                        long freq;
                        if (QueryPerformanceFrequency(out freq))
                        {
                            long secs = (stop - start) / freq;
                            if (secs > 10)
                            {
                                XLog.log("IntComboListPart sort seconds: " + secs.ToString());
                            }
                        }
                    }
#endif
                }
                finally
                {
                    nstm.WriteByte((byte)'+');
                }
            }
            break;

            case 'p':     // Batch push/publish...
            {
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                uint gbfree = (uint)(GetCurrentDiskFreeBytes() / 1073741824);
#if DEBUG
                {
                    string computer_name = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
                    if (computer_name == "MAPDDRULE" || computer_name == "MAPDCMILLER" || computer_name == "computer_name")
                    {
                        gbfree = uint.MaxValue;
                    }
                }
#endif
                if (gbfree > 20)
                {
                    int pcount = len / 12;         // size of B12
                    int y      = 0;
                    for (int i = 0; i != pcount; i++)
                    {
                        TimedAdd(buf, y);
                        y += 12;
                    }
                }
                else
                {
                    if (!nofreedisklog)
                    {
                        nofreedisklog = true;
                        XLog.errorlog("Low free disk space; now dropping entries.");
                    }
                }
            }
            break;

            default:
                base.ProcessCommand(nstm, tag);
                break;
            }
        }
コード例 #26
0
        public virtual void Open()
        {
            if (didopen)
            {
                throw new Exception("Attempted to Open after already Open");
            }
            didopen = true;
            string portfilename = null;

            System.IO.FileStream portfile = null;
            try
            {
                Socket     lsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint lipep;
                //int lptests = 0;
                for (; ;)
                {
                    // Try a few ports...
                    lipep = new IPEndPoint(IPAddress.Any, GetSlavePort());
                    try
                    {
                        System.Threading.Mutex lm = new System.Threading.Mutex(false, "DODLL_Listen{2FDD7284-14A8-4f4d-8448-0CE229C1E596}");
                        lm.WaitOne();
                        try
                        {
                            string spf = lipep.Port.ToString() + ".pf";
                            if (System.IO.File.Exists(spf))
                            {
                                continue;
                            }
                            lsock.Bind(lipep);
                            lsock.Listen(2);
                            portfile = new System.IO.FileStream(spf, System.IO.FileMode.Create, System.IO.FileAccess.Write,
                                                                System.IO.FileShare.Delete, 8, System.IO.FileOptions.DeleteOnClose);
                            portfilename = spf;
                        }
                        finally
                        {
                            lm.ReleaseMutex();
                        }
                    }
                    catch (SocketException e)
                    {
                        //if (++lptests < 4)
                        {
                            continue;
                        }
                        //throw new Exception(e.ToString() + "  [Note: ensure port " + lipep.Port.ToString() + " is free / not blocked (ports >= " + SlavePortMin.ToString() + ")]");
                    }
                    break;
                }
#if DEBUG
                this.OpenPort = lipep.Port;
                lock (AllOpenPorts)
                {
                    AllOpenPorts.Add(this.OpenPort);
                }
#endif

                foreach (SlaveInfo slave in dslaves)
                {
                    NetworkStream servStm = null;

#if DEBUGfailtest
                    bool failtest = true;
#else
                    const bool failtest = false;
#endif
                    for (; ;)
                    {
                        Socket servSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        servSock.Connect(slave.blockinfo[3], 55900);

                        servStm = new XNetworkStream(servSock);

                        servStm.WriteByte((byte)'B'); // AddBlock.
                        XContent.SendXContent(servStm, slave.sblockinfo);
                        XContent.SendXContent(servStm, lipep.Port.ToString());
                        XContent.SendXContent(servStm, sjid + ":" + Convert.ToBase64String(Encoding.UTF8.GetBytes(jobdesc)));
                        {
                            int rB = servStm.ReadByte();
                            if ('+' != rB)
                            {
                                if ('_' == rB)
                                {
                                    throw new Exception("Error returned from AddBlock: " + XContent.ReceiveXString(servStm, null));
                                }
                                else
                                {
                                    throw new Exception("AddBlock failure");
                                }
                            }
                        }

                        // 30000000 microseconds == 30 seconds
                        if (failtest || !lsock.Poll(30000000, SelectMode.SelectRead))
                        {
#if DEBUGfailtest
                            failtest = false;
#endif
                            servStm.Close();
                            continue;
                        }
                        Socket slaveSock = lsock.Accept();
                        slave.nstm = new XNetworkStream(slaveSock);
                        break;
                    }
                    if (1 != slave.nstm.ReadByte())
                    {
                        throw new Exception("Sub process connection error: invalid handshake  [Non-sub-process connection?]");
                    }
                    {
                        int    len;
                        byte[] bpid = XContent.ReceiveXBytes(slave.nstm, out len, null);
                        if (len < 4)
                        {
                            throw new Exception("Sub process connection error: invalid SlavePID handshake");
                        }
                        slave.pid = BytesToInt(bpid);
                    }

                    servStm.WriteByte((byte)'\\');
                    servStm.Close(1000);
                }

                lsock.Close();
            }
            catch (FormatException e)
            {
                throw new FormatException("Format error in Open: " + e.ToString());
            }
            catch (Exception e)
            {
                throw new Exception("Error in Open: " + e.ToString() + "  [Note: ensure the Windows service is running]");
            }
            finally
            {
#if DEBUG
                if (0 != this.OpenPort)
                {
                    lock (AllOpenPorts)
                    {
                        AllOpenPorts.Remove(this.OpenPort);
                    }
                }
#endif
                if (null != portfile)
                {
                    System.Threading.Mutex lm = new System.Threading.Mutex(false, "DODLL_Listen{2FDD7284-14A8-4f4d-8448-0CE229C1E596}");
                    lm.WaitOne();
                    try
                    {
                        if (null != portfile)
                        {
                            portfile.Close();
                        }
                        System.IO.File.Delete(portfilename);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        lm.ReleaseMutex();
                    }
                }
            }
        }