private void InternalDump(DumpRequest request, IClient client) { try { // a sub directory for each date is created inside the dump path var date = DateTime.Today.ToString("yyyy-MM-dd"); var fullPath = Path.Combine(request.Path, date); if (!Directory.Exists(fullPath)) { try { Directory.CreateDirectory(fullPath); } catch (Exception) { // ignore (race condition between nodes) } } Parallel.ForEach(_dataStores, ds => ds.Value.Dump(request, ShardIndex)); // only the first node in the cluster should dump the schema as all shards have identical copies if (request.ShardIndex == 0) { var schemaJson = GenerateSchema(); File.WriteAllText(Path.Combine(fullPath, Constants.SchemaFileName), schemaJson); } // save the sequences. Each shard has different values var sb = new StringBuilder(); _jsonSerializer.Serialize(new JsonTextWriter(new StringWriter(sb)), _lastIdByGeneratorName); var dumpSequenceFileName = $"sequence_{ShardIndex:D3}.json"; File.WriteAllText(Path.Combine(fullPath, dumpSequenceFileName), sb.ToString()); client.SendResponse(new NullResponse()); } catch (AggregateException agg) { client.SendResponse(new ExceptionResponse(agg.InnerExceptions.First())); } catch (Exception e) { client.SendResponse(new ExceptionResponse(e)); } }
/// <summary> /// Dump all data to an external file /// </summary> /// <param name="path"></param> public void Dump(string path) { var request = new DumpRequest { Path = path, ShardIndex = ShardIndex }; var response = Channel.SendRequest(request); if (response is ExceptionResponse exResponse) { throw new CacheException("Error while dumping all data", exResponse.Message, exResponse.CallStack); } }
private void InternalDump(DumpRequest request, IClient client) { try { // a sub directory for each date is created inside the dump path var date = DateTime.Today.ToString("yyyy-MM-dd"); var fullPath = Path.Combine(request.Path, date); if (!Directory.Exists(fullPath)) { try { Directory.CreateDirectory(fullPath); } catch (Exception) { // ignore (race condition between nodes) } } Parallel.ForEach(DataStores.Values, ds => ds.Dump(request.Path, ShardIndex)); // only the first node in the cluster should dump the schema as all shards have identical copies if (request.ShardIndex == 0) { var schema = GenerateSchema(); _serviceContainer.SchemaPersistence.SaveSchema(schema, fullPath); } // save the sequences. Each shard has different values var dumpSequenceFileName = $"sequence_{ShardIndex:D3}.json"; var sequencePath = Path.Combine(fullPath, dumpSequenceFileName); _serviceContainer.SequencePersistence.SaveValues(_lastIdByGeneratorName, sequencePath); client.SendResponse(new NullResponse()); } catch (AggregateException agg) { client.SendResponse(new ExceptionResponse(agg.InnerExceptions.First())); } catch (Exception e) { client.SendResponse(new ExceptionResponse(e)); } }