Files
ntpa/Ntp.Process/Cluster.cs
T
2016-04-16 14:19:42 +02:00

183 lines
6.9 KiB
C#

//
// Cluster.cs
//
// Author:
// Carsten Sonne Larsen <cs@innolan.dk>
//
// Copyright (c) 2013-2016 Carsten Sonne Larsen
//
// 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;
using System.Linq;
using System.Threading;
using Ntp.Analyzer.Log;
using Ntp.Interop;
using Ntp.Analyzer.Localize;
namespace Ntp.Process
{
public sealed class Cluster
{
public Cluster (
string name,
Scheduler scheduler,
IEnumerable<IRequest> peers,
LogBase log)
{
this.name = name;
this.scheduler = scheduler;
this.peers = peers;
this.log = log;
this.reload = false;
waitHandle = new EventWaitHandle (false, EventResetMode.AutoReset);
}
private readonly string name;
private readonly Scheduler scheduler;
private readonly IEnumerable<IRequest> peers;
private readonly LogBase log;
private readonly EventWaitHandle waitHandle;
private bool reload;
private bool run;
public bool Reload {
get { return reload; }
}
public void Activate ()
{
bool activated = true;
if (peers.Count () != 0) {
log.WriteLine (LogMessage.ClusterStart, Severity.Info);
}
Thread signalHandler = new Thread (SignalHandler);
signalHandler.Start ();
run = true;
while (run) {
bool otherActive = false;
foreach (IRequest request in peers) {
try {
string answer = request.Send ("ping");
if (answer != null && answer == "*" && activated) {
log.WriteLine (String.Format (
LogMessage.ClusterNodeAlive, request),
Severity.Notice);
} else if (answer != null && answer == "active" && activated) {
log.WriteLine (String.Format (
LogMessage.ClusterNodeActive, request),
Severity.Notice);
} else if (activated) {
log.WriteLine (String.Format (
LogMessage.ClusterNodeDead, request),
Severity.Notice);
}
if (answer != null && answer == "active") {
otherActive = true;
}
} catch (Exception e) {
log.WriteLine (String.Format (
LogMessage.ClusterNodeError, request),
Severity.Warn);
log.WriteLine (e.Message, Severity.Debug);
log.WriteLine (e, Severity.Trace);
}
}
if (!otherActive) {
try {
scheduler.RunOneCycle ();
} catch (Exception e) {
log.WriteLine (LogMessage.ClusterError, Severity.Error);
log.WriteLine (e.Message, Severity.Debug);
log.WriteLine (e, Severity.Trace);
return;
}
} else {
waitHandle.WaitOne (10000);
}
// This node is now active and no longer "activated"
activated = false;
}
scheduler.Stop ();
}
public void SignalHandler ()
{
bool loop = true;
while (loop) {
try {
switch (InterProcess.Wait (name, log)) {
case InterProcess.Signal.Exit:
loop = false;
run = false;
waitHandle.Set ();
scheduler.WaitHandle.Set ();
log.WriteLine (LogMessage.ClusterClosing, Severity.Notice);
break;
case InterProcess.Signal.Reload:
reload = true;
loop = false;
run = false;
waitHandle.Set ();
scheduler.WaitHandle.Set ();
log.WriteLine (LogMessage.ClusterReload, Severity.Notice);
break;
case InterProcess.Signal.Refresh:
Thread thread = new Thread (() => {
LogFactory.Suspend ();
Thread.Sleep (30000);
LogFactory.Resume ();
});
log.WriteLine (LogMessage.ClusterRefresh, Severity.Notice);
thread.Start ();
break;
case InterProcess.Signal.Notify:
// TODO
// log.WriteLine ("Sending notifications.", Severity.Notice);
break;
case InterProcess.Signal.Error:
log.WriteLine (LogMessage.ClusterErrorInterProc, Severity.Error);
loop = false;
run = false;
break;
default:
log.WriteLine (LogMessage.ClusterErrorSignal, Severity.Error);
break;
}
} catch (Exception e) {
log.WriteLine (LogMessage.ClusterErrorHandler, Severity.Error);
log.WriteLine (e.Message, Severity.Debug);
log.WriteLine (e, Severity.Trace);
loop = false;
run = false;
}
}
}
}
}