Example #1
0
        /// <summary>
        /// Writes records to Cassandra
        /// </summary>
        /// <param name="requestStream"></param>
        /// <param name="responseStream"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task WriteStream(IAsyncStreamReader <Record> requestStream,
                                               IServerStreamWriter <RecordAck> responseStream, ServerCallContext context)
        {
            try
            {
                Logger.Info("Writing records to Cassandra...");

                var schema  = _server.WriteSettings.Schema;
                var inCount = 0;

                // get next record to publish while connected and configured
                while (await requestStream.MoveNext(context.CancellationToken) && _server.Connected &&
                       _server.WriteConfigured)
                {
                    var record = requestStream.Current;
                    inCount++;

                    Logger.Debug($"Got record: {record.DataJson}");

                    if (_server.WriteSettings.IsReplication())
                    {
                        var config =
                            JsonConvert.DeserializeObject <ConfigureReplicationFormData>(_server.WriteSettings.Replication
                                                                                         .SettingsJson);

                        // send record to source system
                        // add await for unit testing
                        // removed to allow multiple to run at the same time
                        Task.Run(async() => await Replication.WriteRecord(_sessionFactory, schema, record, config, responseStream), context.CancellationToken);
                    }
                    else
                    {
                        // send record to source system
                        // add await for unit testing
                        // removed to allow multiple to run at the same time
                        Task.Run(async() =>
                                 await Write.WriteRecordAsync(_sessionFactory, schema, record, responseStream),
                                 context.CancellationToken);
                    }
                }

                Logger.Info($"Wrote {inCount} records to Cassandra.");
            }
            catch (Exception e)
            {
                Logger.Error(e, e.Message, context);
            }
        }
Example #2
0
        /// <summary>
        /// Configures the plugin
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task <ConfigureResponse> Configure(ConfigureRequest request, ServerCallContext context)
        {
            Logger.Debug("Got configure request");
            Logger.Debug(JsonConvert.SerializeObject(request, Formatting.Indented));

            // ensure all directories are created
            Directory.CreateDirectory(request.TemporaryDirectory);
            Directory.CreateDirectory(request.PermanentDirectory);
            Directory.CreateDirectory(request.LogDirectory);

            // configure logger
            Logger.SetLogLevel(request.LogLevel);
            Logger.Init(request.LogDirectory);

            _server.Config = request;

            return(Task.FromResult(new ConfigureResponse()));
        }
Example #3
0
        /// <summary>
        /// Prepares writeback settings to write to Cassandra CQL3
        /// </summary>
        /// <param name="request"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task <PrepareWriteResponse> PrepareWrite(PrepareWriteRequest request, ServerCallContext context)
        {
            // Logger.SetLogLevel(Logger.LogLevel.Debug);
            Logger.SetLogPrefix(request.DataVersions.JobId);
            Logger.Info("Preparing write...");
            _server.WriteConfigured = false;

            _server.WriteSettings = new WriteSettings
            {
                CommitSLA    = request.CommitSlaSeconds,
                Schema       = request.Schema,
                Replication  = request.Replication,
                DataVersions = request.DataVersions,
            };

            if (_server.WriteSettings.IsReplication())
            {
                // reconcile job
                Logger.Info($"Starting to reconcile Replication Job {request.DataVersions.JobId}");
                try
                {
                    await Replication.ReconcileReplicationJobAsync(_sessionFactory, request);
                }
                catch (Exception e)
                {
                    Logger.Error(e, e.Message, context);
                    return(new PrepareWriteResponse());
                }

                Logger.Info($"Finished reconciling Replication Job {request.DataVersions.JobId}");
            }

            _server.WriteConfigured = true;

            Logger.Debug(JsonConvert.SerializeObject(_server.WriteSettings, Formatting.Indented));
            Logger.Info("Write prepared.");
            return(new PrepareWriteResponse());
        }