2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.ShardCom;
|
2017-10-10 16:24:36 +00:00
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-23 17:46:59 +00:00
|
|
|
|
using NadekoBot.Common.Collections;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Collections.Generic;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-10-13 04:14:54 +00:00
|
|
|
|
namespace NadekoBot.Core.Services
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
public class ShardsCoordinator
|
|
|
|
|
{
|
2017-10-23 17:46:59 +00:00
|
|
|
|
private class ShardsCoordinatorQueue
|
|
|
|
|
{
|
|
|
|
|
private readonly object _locker = new object();
|
|
|
|
|
private readonly HashSet<int> _set = new HashSet<int>();
|
|
|
|
|
private readonly Queue<int> _queue = new Queue<int>();
|
|
|
|
|
public int Count => _queue.Count;
|
|
|
|
|
|
|
|
|
|
public void Enqueue(int i)
|
|
|
|
|
{
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
if (_set.Add(i))
|
|
|
|
|
_queue.Enqueue(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 07:07:56 +00:00
|
|
|
|
public bool TryPeek(out int id)
|
|
|
|
|
{
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
return _queue.TryPeek(out id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 17:46:59 +00:00
|
|
|
|
public bool TryDequeue(out int id)
|
|
|
|
|
{
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
if (_queue.TryDequeue(out id))
|
|
|
|
|
{
|
|
|
|
|
_set.Remove(id);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-17 19:42:36 +00:00
|
|
|
|
private readonly BotCredentials _creds;
|
2017-10-10 16:24:36 +00:00
|
|
|
|
private readonly string _key;
|
2017-07-18 02:25:30 +00:00
|
|
|
|
private readonly Process[] _shardProcesses;
|
2017-06-22 21:59:54 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly Logger _log;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
private readonly int _curProcessId;
|
2017-10-10 16:24:36 +00:00
|
|
|
|
private readonly ConnectionMultiplexer _redis;
|
|
|
|
|
private ShardComMessage _defaultShardState;
|
2017-07-05 15:38:38 +00:00
|
|
|
|
|
2017-10-23 17:46:59 +00:00
|
|
|
|
private ShardsCoordinatorQueue _shardStartQueue =
|
|
|
|
|
new ShardsCoordinatorQueue();
|
|
|
|
|
|
|
|
|
|
private ConcurrentHashSet<int> _shardRestartWaitingList =
|
|
|
|
|
new ConcurrentHashSet<int>();
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
public ShardsCoordinator()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
//load main stuff
|
2017-10-29 19:06:48 +00:00
|
|
|
|
LogSetup.SetupLogger(-1);
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-07-17 19:42:36 +00:00
|
|
|
|
_creds = new BotCredentials();
|
2017-10-13 00:21:39 +00:00
|
|
|
|
|
|
|
|
|
_log.Info("Starting NadekoBot v" + StatsService.BotVersion);
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_key = _creds.RedisKey();
|
2017-10-13 00:21:39 +00:00
|
|
|
|
_redis = ConnectionMultiplexer.Connect("127.0.0.1");
|
2017-10-09 22:04:02 +00:00
|
|
|
|
|
2017-11-05 12:28:08 +00:00
|
|
|
|
new RedisImagesCache(_redis, _creds).Reload(); //reload images into redis
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
//setup initial shard statuses
|
|
|
|
|
_defaultShardState = new ShardComMessage()
|
|
|
|
|
{
|
|
|
|
|
ConnectionState = Discord.ConnectionState.Disconnected,
|
|
|
|
|
Guilds = 0,
|
2017-10-23 17:46:59 +00:00
|
|
|
|
Time = DateTime.UtcNow
|
2017-10-10 16:24:36 +00:00
|
|
|
|
};
|
|
|
|
|
var db = _redis.GetDatabase();
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//clear previous statuses
|
|
|
|
|
db.KeyDelete(_key + "_shardstats");
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_shardProcesses = new Process[_creds.TotalShards];
|
|
|
|
|
for (int i = 0; i < _creds.TotalShards; i++)
|
2017-10-09 22:04:02 +00:00
|
|
|
|
{
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//add it to the list of shards which should be started
|
|
|
|
|
#if DEBUG
|
|
|
|
|
if (i > 0)
|
|
|
|
|
_shardStartQueue.Enqueue(i);
|
2017-10-24 16:50:51 +00:00
|
|
|
|
else
|
|
|
|
|
_shardProcesses[i] = Process.GetCurrentProcess();
|
2017-10-23 17:46:59 +00:00
|
|
|
|
#else
|
2017-10-24 16:50:51 +00:00
|
|
|
|
_shardStartQueue.Enqueue(i);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
#endif
|
2017-10-29 03:46:42 +00:00
|
|
|
|
//set the shard's initial state in redis cache
|
|
|
|
|
var msg = _defaultShardState.Clone();
|
|
|
|
|
msg.ShardId = i;
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//this is to avoid the shard coordinator thinking that
|
2017-11-22 07:07:56 +00:00
|
|
|
|
//the shard is unresponsive while starting up
|
2017-10-29 03:46:42 +00:00
|
|
|
|
var delay = 45;
|
|
|
|
|
#if GLOBAL_NADEKO
|
|
|
|
|
delay = 180;
|
|
|
|
|
#endif
|
|
|
|
|
msg.Time = DateTime.UtcNow + TimeSpan.FromSeconds(delay * (i + 1));
|
2017-10-15 08:20:49 +00:00
|
|
|
|
db.ListRightPush(_key + "_shardstats",
|
2017-10-29 03:46:42 +00:00
|
|
|
|
JsonConvert.SerializeObject(msg),
|
2017-10-15 08:20:49 +00:00
|
|
|
|
flags: CommandFlags.FireAndForget);
|
2017-10-09 22:04:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
_curProcessId = Process.GetCurrentProcess().Id;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//subscribe to shardcoord events
|
2017-10-10 16:24:36 +00:00
|
|
|
|
var sub = _redis.GetSubscriber();
|
2017-10-23 17:46:59 +00:00
|
|
|
|
|
|
|
|
|
//send is called when shard status is updated. Every 7.5 seconds atm
|
2017-10-10 16:24:36 +00:00
|
|
|
|
sub.Subscribe(_key + "_shardcoord_send",
|
|
|
|
|
OnDataReceived,
|
|
|
|
|
CommandFlags.FireAndForget);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
|
2017-11-22 07:07:56 +00:00
|
|
|
|
//called to stop the shard, although the shard will start again when it finds out it's dead
|
2017-10-10 16:24:36 +00:00
|
|
|
|
sub.Subscribe(_key + "_shardcoord_stop",
|
|
|
|
|
OnStop,
|
|
|
|
|
CommandFlags.FireAndForget);
|
2017-10-22 14:09:51 +00:00
|
|
|
|
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//called kill the bot
|
2017-10-22 14:09:51 +00:00
|
|
|
|
sub.Subscribe(_key + "_die",
|
|
|
|
|
(ch, x) => Environment.Exit(0),
|
|
|
|
|
CommandFlags.FireAndForget);
|
2017-10-10 16:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStop(RedisChannel ch, RedisValue data)
|
|
|
|
|
{
|
|
|
|
|
var shardId = JsonConvert.DeserializeObject<int>(data);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
OnStop(shardId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStop(int shardId)
|
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
var db = _redis.GetDatabase();
|
2017-10-29 03:46:42 +00:00
|
|
|
|
var msg = _defaultShardState.Clone();
|
|
|
|
|
msg.ShardId = shardId;
|
2017-10-15 08:20:49 +00:00
|
|
|
|
db.ListSetByIndex(_key + "_shardstats",
|
2017-10-10 16:24:36 +00:00
|
|
|
|
shardId,
|
2017-10-29 03:46:42 +00:00
|
|
|
|
JsonConvert.SerializeObject(msg),
|
2017-10-10 16:24:36 +00:00
|
|
|
|
CommandFlags.FireAndForget);
|
|
|
|
|
var p = _shardProcesses[shardId];
|
|
|
|
|
_shardProcesses[shardId] = null;
|
|
|
|
|
try { p?.Kill(); } catch { }
|
|
|
|
|
try { p?.Dispose(); } catch { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDataReceived(RedisChannel ch, RedisValue data)
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-10 16:24:36 +00:00
|
|
|
|
var msg = JsonConvert.DeserializeObject<ShardComMessage>(data);
|
|
|
|
|
if (msg == null)
|
|
|
|
|
return;
|
|
|
|
|
var db = _redis.GetDatabase();
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//sets the shard state
|
2017-10-15 08:20:49 +00:00
|
|
|
|
db.ListSetByIndex(_key + "_shardstats",
|
2017-10-10 16:24:36 +00:00
|
|
|
|
msg.ShardId,
|
|
|
|
|
data,
|
|
|
|
|
CommandFlags.FireAndForget);
|
|
|
|
|
if (msg.ConnectionState == Discord.ConnectionState.Disconnected
|
|
|
|
|
|| msg.ConnectionState == Discord.ConnectionState.Disconnecting)
|
|
|
|
|
{
|
|
|
|
|
_log.Error("!!! SHARD {0} IS IN {1} STATE !!!", msg.ShardId, msg.ConnectionState.ToString());
|
2017-10-23 17:46:59 +00:00
|
|
|
|
|
|
|
|
|
OnShardUnavailable(msg.ShardId);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// remove the shard from the waiting list if it's on it,
|
|
|
|
|
// because it's connected/connecting now
|
|
|
|
|
_shardRestartWaitingList.TryRemove(msg.ShardId);
|
2017-10-10 16:24:36 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-23 17:46:59 +00:00
|
|
|
|
private void OnShardUnavailable(int shardId)
|
|
|
|
|
{
|
|
|
|
|
//if the shard is dc'd, add it to the restart waiting list
|
|
|
|
|
if (!_shardRestartWaitingList.Add(shardId))
|
|
|
|
|
{
|
|
|
|
|
//if it's already on the waiting list
|
|
|
|
|
//stop the shard
|
|
|
|
|
OnStop(shardId);
|
|
|
|
|
//add it to the start queue (start the shard)
|
|
|
|
|
_shardStartQueue.Enqueue(shardId);
|
|
|
|
|
//remove it from the waiting list
|
|
|
|
|
_shardRestartWaitingList.TryRemove(shardId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 02:23:11 +00:00
|
|
|
|
public async Task RunAsync()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-23 17:46:59 +00:00
|
|
|
|
//this task will complete when the initial start of the shards
|
|
|
|
|
//is complete, but will keep running in order to restart shards
|
|
|
|
|
//which are disconnected for too long
|
|
|
|
|
TaskCompletionSource<bool> tsc = new TaskCompletionSource<bool>();
|
|
|
|
|
var _ = Task.Run(async () =>
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
2017-10-23 17:46:59 +00:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
//start a shard which is scheduled for start every 6 seconds
|
2017-11-22 07:07:56 +00:00
|
|
|
|
while (_shardStartQueue.TryPeek(out var id))
|
2017-10-23 17:46:59 +00:00
|
|
|
|
{
|
|
|
|
|
// if the shard is on the waiting list again
|
|
|
|
|
// remove it since it's starting up now
|
2017-10-10 16:24:36 +00:00
|
|
|
|
|
2017-10-23 17:46:59 +00:00
|
|
|
|
_shardRestartWaitingList.TryRemove(id);
|
|
|
|
|
//if the task is already completed,
|
|
|
|
|
//it means the initial shard starting is done,
|
|
|
|
|
//and this is an auto-restart
|
|
|
|
|
if (tsc.Task.IsCompleted)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Auto-restarting shard {0}", id);
|
|
|
|
|
}
|
|
|
|
|
var p = StartShard(id);
|
2017-11-22 07:07:56 +00:00
|
|
|
|
var toRemove = _shardProcesses[id];
|
|
|
|
|
try { toRemove?.Kill(); } catch { }
|
|
|
|
|
try { toRemove?.Dispose(); } catch { }
|
2017-10-23 17:46:59 +00:00
|
|
|
|
_shardProcesses[id] = p;
|
2017-11-22 07:07:56 +00:00
|
|
|
|
_shardStartQueue.TryDequeue(out var __);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
await Task.Delay(6000).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
tsc.TrySetResult(true);
|
2017-10-24 03:06:06 +00:00
|
|
|
|
await Task.Delay(6000).ConfigureAwait(false);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
while (true);
|
|
|
|
|
// ^ keep checking for shards which need to be restarted
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//restart unresponsive shards
|
|
|
|
|
_ = Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
//after all shards have started initially
|
|
|
|
|
await tsc.Task.ConfigureAwait(false);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2017-10-24 16:50:51 +00:00
|
|
|
|
await Task.Delay(15000).ConfigureAwait(false);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var db = _redis.GetDatabase();
|
|
|
|
|
//get all shards which didn't communicate their status in the last 30 seconds
|
|
|
|
|
var all = db.ListRange(_creds.RedisKey() + "_shardstats")
|
|
|
|
|
.Select(x => JsonConvert.DeserializeObject<ShardComMessage>(x));
|
|
|
|
|
var statuses = all
|
|
|
|
|
.Where(x => x.Time < DateTime.UtcNow - TimeSpan.FromSeconds(30));
|
|
|
|
|
|
|
|
|
|
if (!statuses.Any())
|
|
|
|
|
{
|
2017-10-24 07:19:28 +00:00
|
|
|
|
#if DEBUG
|
2017-10-23 17:46:59 +00:00
|
|
|
|
for (var i = 0; i < _shardProcesses.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var p = _shardProcesses[i];
|
|
|
|
|
if (p == null || p.HasExited)
|
2017-10-24 07:19:28 +00:00
|
|
|
|
{
|
|
|
|
|
_log.Warn("Scheduling shard {0} for restart because it's process is stopped.", i);
|
2017-10-23 17:46:59 +00:00
|
|
|
|
_shardStartQueue.Enqueue(i);
|
2017-10-24 07:19:28 +00:00
|
|
|
|
}
|
2017-10-23 17:46:59 +00:00
|
|
|
|
}
|
2017-10-24 04:35:50 +00:00
|
|
|
|
#endif
|
2017-10-23 17:46:59 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var s in statuses)
|
|
|
|
|
{
|
|
|
|
|
OnStop(s.ShardId);
|
|
|
|
|
_shardStartQueue.Enqueue(s.ShardId);
|
|
|
|
|
|
|
|
|
|
//to prevent shards which are already scheduled for restart to be scheduled again
|
|
|
|
|
s.Time = DateTime.UtcNow + TimeSpan.FromSeconds(30 * _shardStartQueue.Count);
|
|
|
|
|
db.ListSetByIndex(_key + "_shardstats", s.ShardId,
|
|
|
|
|
JsonConvert.SerializeObject(s), CommandFlags.FireAndForget);
|
|
|
|
|
_log.Warn("Shard {0} is scheduled for a restart because it's unresponsive.", s.ShardId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { _log.Error(ex); throw; }
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await tsc.Task.ConfigureAwait(false);
|
|
|
|
|
return;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 16:24:36 +00:00
|
|
|
|
private Process StartShard(int shardId)
|
|
|
|
|
{
|
|
|
|
|
return Process.Start(new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
FileName = _creds.ShardRunCommand,
|
|
|
|
|
Arguments = string.Format(_creds.ShardRunArguments, shardId, _curProcessId, "")
|
|
|
|
|
});
|
|
|
|
|
// last "" in format is for backwards compatibility
|
|
|
|
|
// because current startup commands have {2} in them probably
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-20 02:23:11 +00:00
|
|
|
|
public async Task RunAndBlockAsync()
|
2017-06-19 13:42:10 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-06-20 02:23:11 +00:00
|
|
|
|
await RunAsync().ConfigureAwait(false);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Error(ex);
|
2017-10-10 16:24:36 +00:00
|
|
|
|
foreach (var p in _shardProcesses)
|
|
|
|
|
{
|
|
|
|
|
try { p.Kill(); } catch { }
|
|
|
|
|
try { p.Dispose(); } catch { }
|
|
|
|
|
}
|
|
|
|
|
return;
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
2017-07-06 17:30:22 +00:00
|
|
|
|
|
|
|
|
|
await Task.Delay(-1);
|
2017-06-19 13:42:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|