void _SimpleCmdPinSM(char tagchar, string arg) { _StartCmdPinSM(tagchar); XContent.SendXContent(pinstm, arg); int ich = pinstm.ReadByte(); if ('+' != ich) { string errmsg = null; try { if ('-' == ich) { errmsg = XContent.ReceiveXString(pinstm, null); } } catch { } if (null != errmsg) { throw new Exception("MemCachePin service error: " + errmsg + " {" + tagchar + "}"); } throw new Exception("MemCachePin service did not return a success signal {" + tagchar + "}"); } }
static void _MemCacheRelease(dfs.DfsFile df, bool force) { dfs dc = LoadDfsConfig(); string exception = ""; string[] slaves = dc.Slaves.SlaveList.Split(';'); { //foreach (string slave in slaves) MySpace.DataMining.Threading.ThreadTools <string> .Parallel( new Action <string>( delegate(string slave) { System.Net.Sockets.NetworkStream nstm = Surrogate.ConnectService(slave); nstm.WriteByte((byte)'C'); nstm.WriteByte((byte)'r'); XContent.SendXContent(nstm, df.Name); XContent.SendXContent(nstm, new byte[1] { (byte)(force ? 1 : 0) }); int ich = nstm.ReadByte(); if ('+' != ich) { string errmsg = null; if ('-' == ich) { try { errmsg = XContent.ReceiveXString(nstm, null); } catch { } } lock (slaves) { string newexception; if (null != errmsg) { newexception = ("Error received from DO service during MemCache rollback from " + slave + ": " + errmsg); } else { newexception = ("Did not receive a success signal from DO service during MemCache rollback from " + slave); } if (string.IsNullOrEmpty(exception) || -1 != exception.IndexOf("MemCacheWarning")) { exception = newexception; } } } }), slaves, slaves.Length); } if (!string.IsNullOrEmpty(exception)) { throw new Exception(exception); } }
static void _MemCacheLoad(dfs.DfsFile df) { dfs dc = LoadDfsConfig(); string[] slaves = dc.Slaves.SlaveList.Split(';'); { //foreach (string slave in slaves) MySpace.DataMining.Threading.ThreadTools <string> .Parallel( new Action <string>( delegate(string slave) { System.Net.Sockets.NetworkStream nstm = Surrogate.ConnectService(slave); nstm.WriteByte((byte)'C'); nstm.WriteByte((byte)'l'); XContent.SendXContent(nstm, df.Name); int ich = nstm.ReadByte(); if ('+' != ich) { string errmsg = null; if ('-' == ich) { try { errmsg = XContent.ReceiveXString(nstm, null); } catch { } } if (null != errmsg) { throw new Exception("Error received from DO service during MemCache load from " + slave + ": " + errmsg); } throw new Exception("Did not receive a success signal from DO service during MemCache load from " + slave); } }), slaves, slaves.Length); } }
public static void MemCacheFlush(string mcname) { if (mcname.StartsWith("dfs://", StringComparison.OrdinalIgnoreCase)) { mcname = mcname.Substring(6); } dfs dc = LoadDfsConfig(); dfs.DfsFile df = dc.FindAny(mcname); if (null == df || df.MemCache == null) { Console.Error.WriteLine("Error: '{0}' is not a MemCache", (null == df ? mcname : df.Name)); SetFailure(); return; } string tempdfsname = mcname + Guid.NewGuid() + dfs.TEMP_FILE_MARKER; string[] slaves = dc.Slaves.SlaveList.Split(';'); long dfsfilesize = 0; { string tempfp = "bp-mcflush" + Guid.NewGuid() + ".tmp"; try { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(tempfp)) { //foreach (string slave in slaves) MySpace.DataMining.Threading.ThreadTools <string> .Parallel( new Action <string>( delegate(string slave) { System.Net.Sockets.NetworkStream nstm = Surrogate.ConnectService(slave); nstm.WriteByte((byte)'C'); nstm.WriteByte((byte)'f'); XContent.SendXContent(nstm, df.Name); int ich = nstm.ReadByte(); if ('+' != ich) { string errmsg = null; if ('-' == ich) { try { errmsg = XContent.ReceiveXString(nstm, null); } catch { } } if (null != errmsg) { throw new Exception("Error received from DO service during MemCache commit: " + errmsg); } throw new Exception("Did not receive a success signal from DO service during MemCache commit"); } // flushinfos: chunk name, chunk size (without header) string[] flushinfos = XContent.ReceiveXString(nstm, null).Split( new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); foreach (string flushinfo in flushinfos) { lock (slaves) { sw.WriteLine("{0} {1}", slave, flushinfo); dfsfilesize += int.Parse(flushinfo.Split(' ')[1]); } } }), slaves, slaves.Length); } DfsBulkPut(new string[] { tempfp, tempdfsname, "rbin@" + df.RecordLength }); } finally { try { System.IO.File.Delete(tempfp); } catch { } } } Dictionary <string, bool> newchunknames = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase); using (LockDfsMutex()) { dc = LoadDfsConfig(); dfs.DfsFile df2 = dc.FindAny(tempdfsname); if (null == df2) { throw new Exception("DEBUG: Temp DFS file not found: " + tempdfsname); } for (int i = 0; i < dc.Files.Count; i++) { if (0 == string.Compare(dc.Files[i].Name, mcname, true)) { dc.Files.RemoveAt(i); break; } } foreach (dfs.DfsFile.FileNode fn in df2.Nodes) { newchunknames[fn.Name] = true; } df2.MemCache = df.MemCache; df2.Size = dfsfilesize; df2.Name = df.Name; UpdateDfsXml(dc); } { // Just kill the old chunks, not the MemCache stuff. List <string> delfnodes = new List <string>(); { //Collect file node paths. for (int dn = 0; dn < df.Nodes.Count; dn++) { if (newchunknames.ContainsKey(df.Nodes[dn].Name)) { continue; } foreach (string chost in df.Nodes[dn].Host.Split(';')) { delfnodes.Add(NetworkPathForHost(chost) + @"\" + df.Nodes[dn].Name); } } } _KillDataFileChunksInternal_unlocked_mt(delfnodes); } }
static void ReceiveThreadProc(object _host) { string host = (string)_host; byte[] buf = new byte[8 + 4 + 4 + 4]; //! Entry.LongToBytes(jid, buf, 0); Entry.ToBytes(heartbeattimeout, buf, 8); Entry.ToBytes(heartbeatretries, buf, 8 + 4); Entry.ToBytes(tattletimeout, buf, 8 + 4 + 4); try { System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); sock.Connect(host, 55900); System.Net.Sockets.NetworkStream netsm = new XNetworkStream(sock); lock (hostToNetsms) { hostToNetsms[host] = netsm; } netsm.WriteByte((byte)'h'); // start vitals reporter XContent.SendXContent(netsm, buf); while (!stop) { int ib = netsm.ReadByte(); if (ib == -1) { break; } else if (ib == 'h') //heartbeat { #if FAILOVER_DEBUG Log("Heartbeat thread received a heartbeat from:" + host); #endif lock (heartBeats) { heartBeats[host] = DateTime.Now.Ticks; } } else if (ib == (byte)'e') //tattled bad hosts { string strbhs = XContent.ReceiveXString(netsm, buf); #if FAILOVER_DEBUG Log("Heartbeat thread received tattled host from:" + host + ";tattled host=" + strbhs); #endif string[] bhs = strbhs.ToLower().Split(';'); lock (rogueHosts) { foreach (string bh in bhs) { if (!rogueHosts.ContainsKey(bh)) { rogueHosts.Add(bh, "Tattled by " + host); } } } #if FAILOVER_DEBUG Log("VitalMonitor.rogueHosts:" + string.Join(";", (new List <string>(rogueHosts.Keys).ToArray()))); #endif } else { #if FAILOVER_DEBUG Log("VitalMonitor receiving thread obtained unknown heartbeat: " + ib.ToString() + " from host:" + host); #endif throw new Exception("VitalMonitor receiving thread obtained unknown heartbeat: " + ib.ToString() + " from host:" + host); } } } catch (Exception e) { LogOutputToFile("VitalMonitor receiver thread exception:" + e.ToString()); } }