Exemplo n.º 1
0
 public void UpdateLeaders(Object state)
 {
     foreach (Leader leader in leaders)
     {
         TimeSpan elapsed = DateTime.Now - leader.LastUpdated;
         if (elapsed.TotalSeconds < maxDelayinSeconds)
         {
             continue;
         }
         // update leader value in the leader db
         try
         {
             using (SyncMonDB db = new SyncMonDB(dbCxnMap[leader.Name]))
             {
                 //if the leader value doesn't exist, insert, otherwise, update
                 SyncAgent row = (from r in db.SyncAgents where r.Name == leader.Name select r).SingleOrDefault();
                 if (row == null)
                 {
                     row             = new SyncAgent();
                     row.Name        = leader.Name;
                     row.Value       = (new Random()).Next(0, 1000);
                     row.LastUpdated = DateTime.Now;
                     db.SyncAgents.Add(row);
                 }
                 else
                 {
                     row.Value       = (new Random()).Next(0, 1000);
                     row.LastUpdated = DateTime.Now;
                 }
                 db.SaveChanges();
             }
         } catch (Exception e)
         {
             Trace.TraceError("Failed to update leader {0}. {1}", leader.Name, e.Message);
         }
     }
 }
Exemplo n.º 2
0
 public void UpdateLeaders(Object state)
 {
     foreach (Leader leader in leaders)
     {
         TimeSpan elapsed = DateTime.Now - leader.LastUpdated; 
         if (elapsed.TotalSeconds < maxDelayinSeconds) continue;
         // update leader value in the leader db
         try
         {
             using (SyncMonDB db = new SyncMonDB(dbCxnMap[leader.Name]))
             {
                 //if the leader value doesn't exist, insert, otherwise, update
                 SyncAgent row = (from r in db.SyncAgents where r.Name == leader.Name select r).SingleOrDefault();
                 if (row == null)
                 {
                     row = new SyncAgent();
                     row.Name = leader.Name;
                     row.Value = (new Random()).Next(0, 1000);
                     row.LastUpdated = DateTime.Now;
                     db.SyncAgents.Add(row);
                 }
                 else
                 {
                     row.Value = (new Random()).Next(0, 1000);
                     row.LastUpdated = DateTime.Now;
                 }
                 db.SaveChanges();
             }
         } catch (Exception e)
         {
             Trace.TraceError("Failed to update leader {0}. {1}", leader.Name, e.Message);
         }
     }
 }
Exemplo n.º 3
0
        private void QueryState(Object state)
        {
            //get the Sync table from each DB
            Dictionary<string, Dictionary<string, SyncAgent>> syncTables = new Dictionary<string,Dictionary<string, SyncAgent>>();
            foreach (string key in dbCxnMap.Keys)
            {
                try
                {
                    using (SyncMonDB db = new SyncMonDB(dbCxnMap[key]))
                    {
                        Dictionary<string, SyncAgent> rows = new Dictionary<string, SyncAgent>();
                        IEnumerable<SyncAgent> query = (from r in db.SyncAgents select r);
                        foreach (SyncAgent row in query)
                        {
                            rows[row.Name] = row;
                        }
                        syncTables[key] = rows;
                    }
                } catch (Exception e)
                {
                    Trace.TraceError("failed to access db {0}: {1}", key, e.Message);
                    syncTables[key] = null;
                }
            }
            //update the state in memory
            foreach (Leader leader in leaders)
            {
                // find leader's db
                Dictionary<string, SyncAgent> rows = syncTables[leader.Name];
                if (rows == null) continue; //can't connect to leader's db
                // find leader's value in leader's db
                leader.Value = rows[leader.Name].Value;
                leader.LastUpdated = rows[leader.Name].LastUpdated;
                leader.LastUpdatedStr = leader.LastUpdated.ToString("g");
                foreach(Follower follower in leader.Followers)
                {
                    // find follower's db
                    Dictionary<string, SyncAgent> flrows = syncTables[follower.Name];
                    if (flrows == null) continue; //can't connect to follower's db
                    follower.Status = StatusCode[2]; //default is out-of-date
                    if (!flrows.ContainsKey(leader.Name)) continue; //follower doesn't have leader info yet
                    // find leader's value in follower's db
                    follower.Value = flrows[leader.Name].Value;
                    follower.LastUpdated = flrows[leader.Name].LastUpdated;
                    follower.LastUpdatedStr = follower.LastUpdated.ToString("g");
                    follower.SyncDelayed = leader.LastUpdated - follower.LastUpdated;
                    follower.SyncDelayedStr = follower.SyncDelayed.ToString(@"dd\.hh\:mm\:ss") + " days";
                    if (follower.Value == leader.Value)
                        follower.Status = StatusCode[0];
                    else if (follower.SyncDelayed.TotalSeconds < maxDelayinSeconds)
                        follower.Status = StatusCode[1];
                }
            };

            DumpState();
        }
Exemplo n.º 4
0
        private void QueryState(Object state)
        {
            //get the Sync table from each DB
            Dictionary <string, Dictionary <string, SyncAgent> > syncTables = new Dictionary <string, Dictionary <string, SyncAgent> >();

            foreach (string key in dbCxnMap.Keys)
            {
                try
                {
                    using (SyncMonDB db = new SyncMonDB(dbCxnMap[key]))
                    {
                        Dictionary <string, SyncAgent> rows  = new Dictionary <string, SyncAgent>();
                        IEnumerable <SyncAgent>        query = (from r in db.SyncAgents select r);
                        foreach (SyncAgent row in query)
                        {
                            rows[row.Name] = row;
                        }
                        syncTables[key] = rows;
                    }
                } catch (Exception e)
                {
                    Trace.TraceError("failed to access db {0}: {1}", key, e.Message);
                    syncTables[key] = null;
                }
            }
            //update the state in memory
            foreach (Leader leader in leaders)
            {
                // find leader's db
                Dictionary <string, SyncAgent> rows = syncTables[leader.Name];
                if (rows == null)
                {
                    continue;               //can't connect to leader's db
                }
                // find leader's value in leader's db
                leader.Value          = rows[leader.Name].Value;
                leader.LastUpdated    = rows[leader.Name].LastUpdated;
                leader.LastUpdatedStr = leader.LastUpdated.ToString("g");
                foreach (Follower follower in leader.Followers)
                {
                    // find follower's db
                    Dictionary <string, SyncAgent> flrows = syncTables[follower.Name];
                    if (flrows == null)
                    {
                        continue;                    //can't connect to follower's db
                    }
                    follower.Status = StatusCode[2]; //default is out-of-date
                    if (!flrows.ContainsKey(leader.Name))
                    {
                        continue;                                   //follower doesn't have leader info yet
                    }
                    // find leader's value in follower's db
                    follower.Value          = flrows[leader.Name].Value;
                    follower.LastUpdated    = flrows[leader.Name].LastUpdated;
                    follower.LastUpdatedStr = follower.LastUpdated.ToString("g");
                    follower.SyncDelayed    = leader.LastUpdated - follower.LastUpdated;
                    follower.SyncDelayedStr = follower.SyncDelayed.ToString(@"dd\.hh\:mm\:ss") + " days";
                    if (follower.Value == leader.Value)
                    {
                        follower.Status = StatusCode[0];
                    }
                    else if (follower.SyncDelayed.TotalSeconds < maxDelayinSeconds)
                    {
                        follower.Status = StatusCode[1];
                    }
                }
            }
            ;

            DumpState();
        }