Beispiel #1
0
        public SmartBulkCopy(SmartBulkCopyConfiguration config, ILogger logger)
        {
            _logger = logger;
            _config = config;

            AssemblyLoadContext.Default.Resolving += OnAssemblyResolve;
        }
Beispiel #2
0
        static async Task <int> Main(string[] args)
        {
            int result = 0;
            var logger = LogManager.GetCurrentClassLogger();

            try
            {
                SmartBulkCopyConfiguration bulkCopyConfig;
                if (args.Length > 0)
                {
                    bulkCopyConfig = SmartBulkCopyConfiguration.LoadFromConfigFile(args[0]);
                }
                else
                {
                    bulkCopyConfig = SmartBulkCopyConfiguration.LoadFromConfigFile();
                }

                var sbc = new SmartBulkCopy(bulkCopyConfig, logger);
                result = await sbc.Copy();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Stopped program because of exception.");
                result = 1;
            }
            finally
            {
                LogManager.Shutdown();
            }

            return(result);
        }
Beispiel #3
0
        public SmartBulkCopy(SmartBulkCopyConfiguration config, ILogger logger)
        {
            _logger = logger;
            _config = config;

            var v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            _logger.Info($"SmartBulkCopy engine v. {v}");
        }
Beispiel #4
0
        public static SmartBulkCopyConfiguration LoadFromConfigFile(string configFile)
        {
            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile(configFile, optional: false, reloadOnChange: false)
                         .Build();

            var sbcc = new SmartBulkCopyConfiguration();

            sbcc.SourceConnectionString      = config["source:connection-string"];
            sbcc.DestinationConnectionString = config["destination:connection-string"];
            sbcc.BatchSize         = int.Parse(config?["options:batch-size"] ?? sbcc.BatchSize.ToString());
            sbcc.LogicalPartitions = int.Parse(config?["options:logical-partitions"] ?? sbcc.LogicalPartitions.ToString());
            sbcc.MaxParallelTasks  = int.Parse(config?["options:tasks"] ?? sbcc.MaxParallelTasks.ToString());
            sbcc.TruncateTables    = bool.Parse(config?["options:truncate-tables"] ?? sbcc.TruncateTables.ToString());

            var safeCheck = config?["options:safe-check"];

            if (!string.IsNullOrEmpty(safeCheck))
            {
                switch (safeCheck.ToLower())
                {
                case "none": sbcc.SafeCheck = SafeCheck.None;
                    break;

                case "read-only":
                case "readonly": sbcc.SafeCheck = SafeCheck.ReadOnly;
                    break;

                case "snapshot": sbcc.SafeCheck = SafeCheck.Snapshot;
                    break;

                default:
                    throw new ArgumentException("Option safe-check can only contain 'none', 'readonly' or 'snapshot' values.");
                }
            }

            var tablesArray = config.GetSection("tables").GetChildren();

            foreach (var t in tablesArray)
            {
                sbcc.TablesToCopy.Add(t.Value);
            }

            return(sbcc);
        }
Beispiel #5
0
        static async Task <int> Main(string[] args)
        {
            int result = 0;
            var logger = LogManager.GetCurrentClassLogger();

            var v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            logger.Info($"Smart Bulk Copy v. {v}");

            try
            {
                SmartBulkCopyConfiguration bulkCopyConfig;
                if (args.Length > 0)
                {
                    bulkCopyConfig = SmartBulkCopyConfiguration.LoadFromConfigFile(args[0], logger);
                }
                else
                {
                    bulkCopyConfig = SmartBulkCopyConfiguration.LoadFromConfigFile(logger);
                }

                var sbc = new SmartBulkCopy(bulkCopyConfig, logger);
                result = await sbc.Copy();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Stopped program because of exception.");
                result = 1;
            }
            finally
            {
                LogManager.Shutdown();
            }

            return(result);
        }
Beispiel #6
0
        public static SmartBulkCopyConfiguration LoadFromConfigFile(string configFile, ILogger logger)
        {
            // If config file is not found, automatically add .json extension
            if (!File.Exists(Path.Combine(Directory.GetCurrentDirectory(), configFile)))
            {
                if (Path.GetExtension(configFile) != ".json")
                {
                    configFile += ".json";
                }
            }

            logger.Info($"Loading configuration from: {configFile}...");

            var config = new ConfigurationBuilder()
                         .SetBasePath(Directory.GetCurrentDirectory())
                         .AddJsonFile(configFile, optional: false, reloadOnChange: false)
                         .Build();

            var sbcc = new SmartBulkCopyConfiguration();

            sbcc.SourceConnectionString      = config["source:connection-string"] ?? Environment.GetEnvironmentVariable("source-connection-string");
            sbcc.DestinationConnectionString = config["destination:connection-string"] ?? Environment.GetEnvironmentVariable("destination-connection-string");
            sbcc.CommandTimeOut      = int.Parse(config?["options:command-timeout"] ?? sbcc.CommandTimeOut.ToString());
            sbcc.BatchSize           = int.Parse(config?["options:batch-size"] ?? sbcc.BatchSize.ToString());
            sbcc.MaxParallelTasks    = int.Parse(config?["options:tasks"] ?? sbcc.MaxParallelTasks.ToString());
            sbcc.TruncateTables      = bool.Parse(config?["options:truncate-tables"] ?? sbcc.TruncateTables.ToString());
            sbcc.SyncIdentity        = bool.Parse(config?["options:sync-identity"] ?? sbcc.SyncIdentity.ToString());
            sbcc.RetryMaxAttempt     = int.Parse(config?["options:retry-connection:max-attempt"] ?? sbcc.RetryMaxAttempt.ToString());
            sbcc.RetryDelayIncrement = int.Parse(config?["options:retry-connection:delay-increment"] ?? sbcc.RetryDelayIncrement.ToString());

            var logicalPartitions           = (config?["options:logical-partitions"] ?? String.Empty).ToLower().Trim();
            int logicalPartitionSizeOrCount = 0;

            if (logicalPartitions == string.Empty || logicalPartitions == "auto")
            {
                sbcc.LogicalPartitioningStrategy = LogicalPartitioningStrategy.Auto;
            }
            else if (logicalPartitions.EndsWith("gb"))
            {
                sbcc.LogicalPartitioningStrategy = LogicalPartitioningStrategy.Size;
                sbcc.LogicalPartitions           = int.Parse(logicalPartitions.Replace("gb", string.Empty));
            }
            else if (int.TryParse(logicalPartitions, out logicalPartitionSizeOrCount))
            {
                sbcc.LogicalPartitioningStrategy = LogicalPartitioningStrategy.Count;
                sbcc.LogicalPartitions           = logicalPartitionSizeOrCount;
            }
            else
            {
                throw new ArgumentException("Option logical-partitions can only contain \"auto\", or a number (eg: 7) or a size in GB (eg: 10GB)");
            }

            var safeCheck = config?["options:safe-check"];

            if (!string.IsNullOrEmpty(safeCheck))
            {
                switch (safeCheck.ToLower())
                {
                case "none": sbcc.SafeCheck = SafeCheck.None;
                    break;

                case "read-only":
                case "readonly": sbcc.SafeCheck = SafeCheck.ReadOnly;
                    break;

                case "snapshot": sbcc.SafeCheck = SafeCheck.Snapshot;
                    break;

                default:
                    throw new ArgumentException("Option safe-check can only contain 'none', 'readonly' or 'snapshot' values.");
                }
            }

            var stopIf = config.GetSection("options:stop-if")?.GetChildren();

            foreach (var s in stopIf)
            {
                if (s.Key == "secondary-indexes" && bool.Parse(s.Value) == false)
                {
                    sbcc.StopIf -= StopIf.SecondaryIndex;
                }
                if (s.Key == "temporal-table" && bool.Parse(s.Value) == false)
                {
                    sbcc.StopIf -= StopIf.TemporalTable;
                }
            }

            // Support for Include and Exclude or fall back to old "include-only" behavior
            var includeTables = config.GetSection("tables:include")?.GetChildren();

            if (includeTables.Count() != 0)
            {
                foreach (var t in includeTables)
                {
                    sbcc.TablesToCopy.Add("+:" + t.Value);
                }

                var excludeTables = config.GetSection("tables:exclude")?.GetChildren();
                if (excludeTables != null)
                {
                    foreach (var t in excludeTables)
                    {
                        sbcc.TablesToCopy.Add("-:" + t.Value);
                    }
                }
            }
            else
            {
                var tablesArray = config.GetSection("tables").GetChildren();
                foreach (var t in tablesArray)
                {
                    sbcc.TablesToCopy.Add(t.Value);
                }
            }

            return(sbcc);
        }
Beispiel #7
0
 public TableAnalyzer(SmartBulkCopyConfiguration config, ILogger logger)
 {
     this._config = config;
     this._logger = logger;
 }