Files
ntpa/Ntp.Process/Cluster.cs
T

137 lines
5.0 KiB
C#
Raw Normal View History

2016-03-05 15:25:01 +01:00
//
// Cluster.cs
//
// Author:
2016-03-06 00:15:13 +01:00
// Carsten Sonne Larsen <cs@innolan.dk>
2016-03-05 15:25:01 +01:00
//
2016-03-13 00:56:33 +01:00
// Copyright (c) 2013-2016 Carsten Sonne Larsen
2016-03-05 15:25:01 +01:00
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
2016-03-06 00:15:13 +01:00
using System.IO;
2016-03-13 00:56:33 +01:00
using System.Security.AccessControl;
using System.Security.Principal;
2016-03-14 21:38:34 +01:00
using System.Threading;
using Ntp.Analyzer.Log;
2016-03-05 15:25:01 +01:00
namespace Ntp.Process
{
public sealed class Cluster
{
2016-03-13 00:56:33 +01:00
public Cluster (
string name,
Scheduler scheduler,
IEnumerable<IRequest> peers,
LogBase log)
2016-03-05 15:25:01 +01:00
{
2016-03-13 00:56:33 +01:00
this.name = name;
2016-03-05 15:25:01 +01:00
this.scheduler = scheduler;
this.peers = peers;
this.log = log;
2016-03-13 00:56:33 +01:00
waitHandle = new EventWaitHandle (false, EventResetMode.AutoReset);
2016-03-05 15:25:01 +01:00
}
2016-03-13 00:56:33 +01:00
private readonly string name;
2016-03-05 15:25:01 +01:00
private readonly Scheduler scheduler;
private readonly IEnumerable<IRequest> peers;
private readonly LogBase log;
2016-03-13 00:56:33 +01:00
private readonly EventWaitHandle waitHandle;
private bool run;
2016-03-05 15:25:01 +01:00
2016-03-06 00:15:13 +01:00
public void Activate ()
2016-03-05 15:25:01 +01:00
{
2016-03-06 00:15:13 +01:00
log.WriteLine ("Starting cluster module.", Severity.Info);
2016-03-05 15:25:01 +01:00
2016-03-13 00:56:33 +01:00
Thread killSwitch = new Thread (KillSwitch);
killSwitch.Start ();
run = true;
// TODO: Move to public scope (ApplicationState)
2016-03-05 15:25:01 +01:00
bool activated = true;
2016-03-13 00:56:33 +01:00
while (run) {
2016-03-05 15:25:01 +01:00
bool otherActive = false;
2016-03-06 00:15:13 +01:00
foreach (IRequest request in peers) {
2016-03-05 15:25:01 +01:00
2016-03-06 00:15:13 +01:00
try {
string answer = request.Send ("ping");
2016-03-05 15:25:01 +01:00
2016-03-06 00:15:13 +01:00
if (answer != null && answer == "*" && activated) {
log.WriteLine ("Cluster node alive: " + request, Severity.Notice);
} else if (answer != null && answer == "active" && activated) {
log.WriteLine ("Cluster node active: " + request, Severity.Notice);
} else if (activated) {
log.WriteLine ("Cluster node dead: " + request, Severity.Notice);
2016-03-05 15:25:01 +01:00
}
2016-03-06 00:15:13 +01:00
if (answer != null && answer == "active") {
2016-03-05 15:25:01 +01:00
otherActive = true;
}
2016-03-14 21:38:34 +01:00
} catch (Exception e) {
2016-03-06 00:15:13 +01:00
log.WriteLine ("Error while contacting cluster node " + request, Severity.Warn);
2016-03-14 21:38:34 +01:00
log.WriteLine (e, Severity.Trace);
2016-03-06 00:15:13 +01:00
}
2016-03-05 15:25:01 +01:00
}
2016-03-06 00:15:13 +01:00
if (!otherActive) {
2016-03-13 00:56:33 +01:00
log.WriteLine ("No other nodes active. Activting node.", Severity.Debug);
2016-03-05 15:25:01 +01:00
2016-03-06 00:15:13 +01:00
try {
scheduler.RunOneCycle ();
} catch (Exception e) {
log.WriteLine ("Error in scheduler module. Arborting.", Severity.Error);
log.WriteLine (e.ToString (), Severity.Error);
2016-03-14 21:38:34 +01:00
log.WriteLine (e, Severity.Trace);
2016-03-05 15:25:01 +01:00
return;
}
2016-03-06 00:15:13 +01:00
} else {
2016-03-13 00:56:33 +01:00
log.WriteLine ("Other node active. Going to fallback mode.", Severity.Debug);
waitHandle.WaitOne (10000);
2016-03-05 15:25:01 +01:00
}
activated = false;
2016-03-13 00:56:33 +01:00
}
}
public void KillSwitch ()
{
try {
string message = InterLock.Wait (name);
if (message != null) {
log.WriteLine ("Error in mutux: " + message, Severity.Error);
}
waitHandle.Set ();
scheduler.WaitHandle.Set ();
run = false;
} catch (Exception e) {
log.WriteLine ("Error in mutux. Arborting.", Severity.Error);
log.WriteLine (e.ToString (), Severity.Error);
2016-03-14 21:38:34 +01:00
log.WriteLine (e, Severity.Trace);
2016-03-05 15:25:01 +01:00
}
2016-03-13 00:56:33 +01:00
log.WriteLine ("Closing down.", Severity.Notice);
2016-03-05 15:25:01 +01:00
}
}
}