Ejemplo n.º 1
0
        internal void fillcurfromnextbuf()
        {
            if (nextbufpos + 12 > nextbufend)
            {
                throw new Exception("Unaligned values (enumeration buffer)");
            }

            currun = 1;

            cur.A = DistObjectBase.BytesToLong(nextbuf, nextbufpos);
            cur.B = DistObjectBase.BytesToInt(nextbuf, nextbufpos + 8);

            nextbufpos += 12;
        }
Ejemplo n.º 2
0
        internal void fillcurfromnextbuf()
        {
            if (nextbufpos + 8 + 2 > nextbufend)
            {
                throw new Exception("Unaligned values (enumeration buffer)");
            }

            currun = MySpace.DataMining.DistributedObjects.Entry.BytesToInt16(nextbuf, nextbufpos);

            cur.A = DistObjectBase.BytesToInt(nextbuf, nextbufpos + 2);
            cur.B = DistObjectBase.BytesToInt(nextbuf, nextbufpos + 2 + 4);

            nextbufpos += 8 + 2;
        }
Ejemplo n.º 3
0
        // 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)");
                }
            }
        }
Ejemplo n.º 4
0
        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
            }
        }
Ejemplo n.º 5
0
 public void SetFromArray(byte[] buf)
 {
     A = DistObjectBase.BytesToLong(buf, 0);
     B = DistObjectBase.BytesToInt(buf, 8);
 }
Ejemplo n.º 6
0
 public void CopyToArray(byte[] buf)
 {
     DistObjectBase.LongToBytes(A, buf, 0);
     DistObjectBase.ToBytes(B, buf, 8);
 }
Ejemplo n.º 7
0
 public void SetFromArray(byte[] buf, int offset)
 {
     A = DistObjectBase.BytesToLong(buf, offset + 0);
     B = DistObjectBase.BytesToInt(buf, offset + 8);
 }
Ejemplo n.º 8
0
 public void CopyToArray(byte[] buf, int offset)
 {
     DistObjectBase.LongToBytes(A, buf, offset + 0);
     DistObjectBase.ToBytes(B, buf, offset + 8);
 }
Ejemplo n.º 9
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 IntComboListPartEnumerator(this);
                            }
                            int offset = 0;
                            if (null == buf || buf.Length < 40)
                            {
                                throw new Exception("Enumeration batch buffer too small!");
                            }
                            //if (uniquecompression) // Compressed...
                            {
                                for (; ;)
                                {
                                    if (!this.enums[enumid].MoveNext())
                                    {
                                        break;
                                    }
                                    B8  b8   = this.enums[enumid].Current;
                                    int nrow = 1;
                                    while (this.enums[enumid].MoveNext())
                                    {
                                        B8 b8next = this.enums[enumid].Current;
                                        if (b8.A != b8next.A || b8.B != b8next.B)  //if (b8 != b8next)
                                        {
                                            this.enums[enumid].MoveBack();         // !
                                            break;
                                        }
                                        nrow++;
                                    }
                                    if (nrow < mindupes)
                                    {
                                        continue;
                                    }
                                    if (nrow > Int16.MaxValue)
                                    {
                                        nrow = Int16.MaxValue;
                                    }
                                    // Using Big Endian!
                                    MySpace.DataMining.DistributedObjects.Entry.Int16ToBytes((Int16)nrow, buf, offset);
                                    b8.CopyToArray(buf, offset + 2);
                                    offset += 8 + 2;
                                    if (offset + 8 + 2 > 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(b8buffer, 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 == "MAPDCLOK")
                    {
                        gbfree = uint.MaxValue;
                    }
                }
#endif
                if (gbfree > 20)
                {
                    int pcount = len / 8;         // size of B8
                    int y      = 0;
                    for (int i = 0; i != pcount; i++)
                    {
                        TimedAdd(buf, y);
                        y += 8;
                    }
                }
                else
                {
                    if (!nofreedisklog)
                    {
                        nofreedisklog = true;
                        XLog.errorlog("Low free disk space; now dropping entries.");
                    }
                }
            }
            break;

            case 'M':
            {
                buf = XContent.ReceiveXBytes(nstm, out len, buf);
                if (4 == len)
                {
                    this.mindupes = DistObjectBase.BytesToInt(buf);
                }
            }
            break;

            default:
                base.ProcessCommand(nstm, tag);
                break;
            }
        }
Ejemplo n.º 10
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());
            }
        }