Example #1
0
        /// <summary>
        /// Start the default Primary and Secondary test silos, plus client in-process,
        /// using the specified silo and client config options.
        /// </summary>
        protected TestingSiloHost(TestingSiloOptions siloOptions, TestingClientOptions clientOptions)
        {
            siloInitOptions   = siloOptions;
            clientInitOptions = clientOptions;

            AppDomain.CurrentDomain.UnhandledException += ReportUnobservedException;

            try
            {
                Initialize(siloOptions, clientOptions);
                string startMsg = "----------------------------- STARTING NEW UNIT TEST SILO HOST: " + GetType().FullName + " -------------------------------------";
                WriteLog(startMsg);
            }
            catch (TimeoutException te)
            {
                throw new TimeoutException("Timeout during test initialization", te);
            }
            catch (Exception ex)
            {
                Exception baseExc = ex.GetBaseException();
                if (baseExc is TimeoutException)
                {
                    throw new TimeoutException("Timeout during test initialization", ex);
                }
                throw new AggregateException(
                          string.Format("Exception during test initialization: {0}",
                                        TraceLogger.PrintException(ex)), ex);
            }
        }
Example #2
0
        /// <summary>
        /// Start the default Primary and Secondary test silos, plus client in-process,
        /// using the specified silo and client config options.
        /// </summary>
        public UnitTestSiloHost(UnitTestSiloOptions siloOptions, UnitTestClientOptions clientOptions)
        {
            this.siloInitOptions   = siloOptions;
            this.clientInitOptions = clientOptions;

            AppDomain.CurrentDomain.UnhandledException += _ReportUnobservedException;

            try
            {
                _Initialize(siloOptions, clientOptions);
                string startMsg = "----------------------------- STARTING NEW UNIT TEST BASE: " + this.GetType().FullName + " -------------------------------------";
                Console.WriteLine(startMsg);
            }
            catch (TimeoutException te)
            {
                throw new TimeoutException("Timeout during test initialization", te);
            }
            catch (Exception ex)
            {
                Exception baseExc = ex.GetBaseException();
                if (baseExc is TimeoutException)
                {
                    throw new TimeoutException("Timeout during test initialization", ex);
                }
                throw new AggregateException(
                          string.Format("Exception during test initialization: {0}",
                                        TraceLogger.PrintException(ex)), ex);
            }
        }
Example #3
0
        private static void InternalUnobservedTaskExceptionHandler(object sender, UnobservedTaskExceptionEventArgs e)
        {
            var aggrException = e.Exception;
            var baseException = aggrException.GetBaseException();
            var tplTask       = (Task)sender;
            var contextObj    = tplTask.AsyncState;
            var context       = contextObj as ISchedulingContext;

            try
            {
                if (unobservedExceptionHandler != null)
                {
                    unobservedExceptionHandler(context, baseException);
                }
            }
            finally
            {
                if (e.Observed)
                {
                    logger.Info(ErrorCode.Runtime_Error_100311, "UnobservedExceptionsHandlerClass caught an UnobservedTaskException which was successfully observed and recovered from. BaseException = {0}. Exception = {1}",
                                baseException.Message, TraceLogger.PrintException(aggrException));
                }
                else
                {
                    var errorStr = String.Format("UnobservedExceptionsHandlerClass Caught an UnobservedTaskException event sent by {0}. Exception = {1}",
                                                 OrleansTaskExtentions.ToString((Task)sender), TraceLogger.PrintException(aggrException));
                    logger.Error(ErrorCode.Runtime_Error_100005, errorStr);
                    logger.Error(ErrorCode.Runtime_Error_100006, "Exception remained UnObserved!!! The subsequent behaivour depends on the ThrowUnobservedTaskExceptions setting in app config and .NET version.");
                }
            }
        }
Example #4
0
        /// <summary>
        /// Ensures that code generation has been run for the provided assembly.
        /// </summary>
        /// <param name="input">
        /// The assembly to generate code for.
        /// </param>
        public void GenerateAndLoadForAssembly(Assembly input)
        {
            try
            {
                RegisterGeneratedCodeTargets(input);
                if (!ShouldGenerateCodeForAssembly(input))
                {
                    TryLoadGeneratedAssemblyFromCache(input);

                    return;
                }

                var timer     = Stopwatch.StartNew();
                var generated = GenerateForAssemblies(new List <Assembly> {
                    input
                }, true);

                var compiled = default(byte[]);
                if (generated.Syntax != null)
                {
                    compiled = CompileAndLoad(generated);
                }

                foreach (var assembly in generated.SourceAssemblies)
                {
                    var generatedAssembly = new GeneratedAssembly {
                        Loaded = true, RawBytes = compiled
                    };
                    CompiledAssemblies.AddOrUpdate(
                        assembly.GetName().FullName,
                        generatedAssembly,
                        (_, __) => generatedAssembly);
                }

                if (Logger.IsVerbose2)
                {
                    Logger.Verbose2(
                        (int)ErrorCode.CodeGenCompilationSucceeded,
                        "Generated code for 1 assembly in {0}ms",
                        timer.ElapsedMilliseconds);
                }
            }
            catch (Exception exception)
            {
                var message = string.Format(
                    "Exception generating code for input assembly {0}\nException: {1}",
                    input.GetName().FullName,
                    TraceLogger.PrintException(exception));
                Logger.Warn((int)ErrorCode.CodeGenCompilationFailed, message, exception);
                throw;
            }
        }
        private void DeployTestingSiloHost(TestingSiloOptions siloOptions, TestingClientOptions clientOptions)
        {
            siloInitOptions   = siloOptions;
            clientInitOptions = clientOptions;

            AppDomain.CurrentDomain.UnhandledException += ReportUnobservedException;

            InitializeLogWriter();
            try
            {
                string startMsg = "----------------------------- STARTING NEW UNIT TEST SILO HOST: " + GetType().FullName + " -------------------------------------";
                WriteLog(startMsg);
                InitializeAsync(siloOptions, clientOptions).Wait();
                UninitializeLogWriter();
                Instance = this;
            }
            catch (TimeoutException te)
            {
                FlushLogToConsole();
                throw new TimeoutException("Timeout during test initialization", te);
            }
            catch (Exception ex)
            {
                StopAllSilos();

                Exception baseExc = ex.GetBaseException();
                FlushLogToConsole();

                if (baseExc is TimeoutException)
                {
                    throw new TimeoutException("Timeout during test initialization", ex);
                }

                // IMPORTANT:
                // Do NOT re-throw the original exception here, also not as an internal exception inside AggregateException
                // Due to the way MS tests works, if the original exception is an Orleans exception,
                // it's assembly might not be loaded yet in this phase of the test.
                // As a result, we will get "MSTest: Unit Test Adapter threw exception: Type is not resolved for member XXX"
                // and will loose the original exception. This makes debugging tests super hard!
                // The root cause has to do with us initializing our tests from Test constructor and not from TestInitialize method.
                // More details: http://dobrzanski.net/2010/09/20/mstest-unit-test-adapter-threw-exception-type-is-not-resolved-for-member/
                throw new Exception(
                          string.Format("Exception during test initialization: {0}",
                                        TraceLogger.PrintException(baseExc)));
            }
        }
Example #6
0
        /// <summary>
        /// Report an error during silo startup.
        /// </summary>
        /// <remarks>
        /// Information on the silo startup issue will be logged to any attached Loggers,
        /// then a timestamped StartupError text file will be written to
        /// the current working directory (if possible).
        /// </remarks>
        /// <param name="exc">Exception which caused the silo startup issue.</param>
        public void ReportStartupError(Exception exc)
        {
            if (string.IsNullOrWhiteSpace(Name))
            {
                Name = "Silo";
            }

            var errMsg = "ERROR starting Orleans silo name=" + Name + " Exception=" + TraceLogger.PrintException(exc);

            if (logger != null)
            {
                logger.Error(ErrorCode.Runtime_Error_100105, errMsg, exc);
            }

            // Dump Startup error to a log file
            var          now        = DateTime.UtcNow;
            const string dateFormat = "yyyy-MM-dd-HH.mm.ss.fffZ";
            var          dateString = now.ToString(dateFormat, CultureInfo.InvariantCulture);
            var          startupLog = Name + "-StartupError-" + dateString + ".txt";

            try
            {
                File.AppendAllText(startupLog, dateString + "Z" + Environment.NewLine + errMsg);
            }
            catch (Exception exc2)
            {
                if (logger != null)
                {
                    logger.Error(ErrorCode.Runtime_Error_100106, "Error writing log file " + startupLog, exc2);
                }
            }

            TraceLogger.Flush();
        }
 internal void ReportError(string msg, Exception exc)
 {
     msg = "*****\t" + msg + "\n   --->" + TraceLogger.PrintException(exc);
     ReportError(msg);
 }
Example #8
0
        /// <summary>
        /// Initialize this Orleans silo for execution with the specified Azure deploymentId
        /// </summary>
        /// <param name="config">If null, Config data will be read from silo config file as normal, otherwise use the specified config data.</param>
        /// <param name="deploymentId">Azure DeploymentId this silo is running under</param>
        /// <param name="connectionString">Azure DataConnectionString. If null, defaults to the DataConnectionString setting from the Azure configuration for this role.</param>
        /// <returns><c>true</c> is the silo startup was successful</returns>
        public bool Start(ClusterConfiguration config, string deploymentId = null, string connectionString = null)
        {
            // Program ident
            Trace.TraceInformation("Starting {0} v{1}", this.GetType().FullName, RuntimeVersion.Current);

            // Check if deployment id was specified
            if (deploymentId == null)
            {
                deploymentId = serviceRuntimeWrapper.DeploymentId;
            }

            // Read endpoint info for this instance from Azure config
            string instanceName = serviceRuntimeWrapper.InstanceName;

            // Configure this Orleans silo instance
            if (config == null)
            {
                host = new SiloHost(instanceName);
                host.LoadOrleansConfig(); // Load config from file + Initializes logger configurations
            }
            else
            {
                host = new SiloHost(instanceName, config); // Use supplied config data + Initializes logger configurations
            }

            IPEndPoint myEndpoint    = serviceRuntimeWrapper.GetIPEndpoint(SiloEndpointConfigurationKeyName);
            IPEndPoint proxyEndpoint = serviceRuntimeWrapper.GetIPEndpoint(ProxyEndpointConfigurationKeyName);

            host.SetSiloType(Silo.SiloType.Secondary);

            int generation = SiloAddress.AllocateNewGeneration();

            // Bootstrap this Orleans silo instance

            myEntry = new SiloInstanceTableEntry
            {
                DeploymentId = deploymentId,
                Address      = myEndpoint.Address.ToString(),
                Port         = myEndpoint.Port.ToString(CultureInfo.InvariantCulture),
                Generation   = generation.ToString(CultureInfo.InvariantCulture),

                HostName  = host.Config.GetConfigurationForNode(host.Name).DNSHostName,
                ProxyPort = (proxyEndpoint != null ? proxyEndpoint.Port : 0).ToString(CultureInfo.InvariantCulture),

                RoleName     = serviceRuntimeWrapper.RoleName,
                InstanceName = instanceName,
                UpdateZone   = serviceRuntimeWrapper.UpdateDomain.ToString(CultureInfo.InvariantCulture),
                FaultZone    = serviceRuntimeWrapper.FaultDomain.ToString(CultureInfo.InvariantCulture),
                StartTime    = TraceLogger.PrintDate(DateTime.UtcNow),

                PartitionKey = deploymentId,
                RowKey       = myEndpoint.Address + "-" + myEndpoint.Port + "-" + generation
            };

            if (connectionString == null)
            {
                connectionString = serviceRuntimeWrapper.GetConfigurationSettingValue(DataConnectionConfigurationSettingName);
            }

            try
            {
                siloInstanceManager = OrleansSiloInstanceManager.GetManager(
                    deploymentId, connectionString).WithTimeout(AzureTableDefaultPolicies.TableCreationTimeout).Result;
            }
            catch (Exception exc)
            {
                var error = String.Format("Failed to create OrleansSiloInstanceManager. This means CreateTableIfNotExist for silo instance table has failed with {0}",
                                          TraceLogger.PrintException(exc));
                Trace.TraceError(error);
                logger.Error(ErrorCode.AzureTable_34, error, exc);
                throw new OrleansException(error, exc);
            }

            // Always use Azure table for membership when running silo in Azure
            host.SetSiloLivenessType(GlobalConfiguration.LivenessProviderType.AzureTable);
            if (host.Config.Globals.ReminderServiceType == GlobalConfiguration.ReminderServiceProviderType.NotSpecified ||
                host.Config.Globals.ReminderServiceType == GlobalConfiguration.ReminderServiceProviderType.ReminderTableGrain)
            {
                host.SetReminderServiceType(GlobalConfiguration.ReminderServiceProviderType.AzureTable);
            }
            host.SetExpectedClusterSize(serviceRuntimeWrapper.RoleInstanceCount);
            siloInstanceManager.RegisterSiloInstance(myEntry);

            // Initialise this Orleans silo instance
            host.SetDeploymentId(deploymentId, connectionString);
            host.SetSiloEndpoint(myEndpoint, generation);
            host.SetProxyEndpoint(proxyEndpoint);

            host.InitializeOrleansSilo();
            logger.Info(ErrorCode.Runtime_Error_100288, "Successfully initialized Orleans silo '{0}' as a {1} node.", host.Name, host.Type);
            return(StartSilo());
        }
Example #9
0
            public async Task <GrainStateRecord> Read(string partitionKey, string rowKey)
            {
                if (logger.IsVerbose3)
                {
                    logger.Verbose3((int)AzureProviderErrorCode.AzureTableProvider_Storage_Reading, "Reading: PartitionKey={0} RowKey={1} from Table={2}", partitionKey, rowKey, TableName);
                }
                try
                {
                    Tuple <GrainStateEntity, string> data = await tableManager.ReadSingleTableEntryAsync(partitionKey, rowKey);

                    if (data == null || data.Item1 == null)
                    {
                        if (logger.IsVerbose2)
                        {
                            logger.Verbose2((int)AzureProviderErrorCode.AzureTableProvider_DataNotFound, "DataNotFound reading: PartitionKey={0} RowKey={1} from Table={2}", partitionKey, rowKey, TableName);
                        }
                        return(null);
                    }
                    GrainStateEntity stateEntity = data.Item1;
                    var record = new GrainStateRecord {
                        Entity = stateEntity, ETag = data.Item2
                    };
                    if (logger.IsVerbose3)
                    {
                        logger.Verbose3((int)AzureProviderErrorCode.AzureTableProvider_Storage_DataRead, "Read: PartitionKey={0} RowKey={1} from Table={2} with ETag={3}", stateEntity.PartitionKey, stateEntity.RowKey, TableName, record.ETag);
                    }
                    return(record);
                }
                catch (Exception exc)
                {
                    if (AzureStorageUtils.TableStorageDataNotFound(exc))
                    {
                        if (logger.IsVerbose2)
                        {
                            logger.Verbose2((int)AzureProviderErrorCode.AzureTableProvider_DataNotFound, "DataNotFound reading (exception): PartitionKey={0} RowKey={1} from Table={2} Exception={3}", partitionKey, rowKey, TableName, TraceLogger.PrintException(exc));
                        }
                        return(null);  // No data
                    }
                    throw;
                }
            }
Example #10
0
        public int RunMain(string[] args)
        {
            ConsoleText.WriteStatus("Orleans-CodeGen - command-line = {0}", Environment.CommandLine);

            if (args.Length < 1)
            {
                Console.WriteLine(
                    "Usage: ClientGenerator.exe <grain interface dll path> [<client dll path>] [<key file>] [<referenced assemblies>]");
                Console.WriteLine(
                    "       ClientGenerator.exe /server <grain dll path> [<factory dll path>] [<key file>] [<referenced assemblies>]");
                return(1);
            }

            try
            {
                var options = new CodeGenOptions();

                // STEP 1 : Parse parameters
                if (args.Length == 1 && args[0].StartsWith("@"))
                {
                    // Read command line args from file
                    string arg      = args[0];
                    string argsFile = arg.Trim('"').Substring(1).Trim('"');
                    Console.WriteLine("Orleans-CodeGen - Reading code-gen params from file={0}", argsFile);
                    AssertWellFormed(argsFile, true);
                    args = File.ReadAllLines(argsFile);
                }
                int i = 1;
                foreach (string a in args)
                {
                    string arg = a.Trim('"').Trim().Trim('"');
                    if (GrainClientGeneratorFlags.Verbose)
                    {
                        Console.WriteLine("Orleans-CodeGen - arg #{0}={1}", i++, arg);
                    }
                    if (string.IsNullOrEmpty(arg) || string.IsNullOrWhiteSpace(arg))
                    {
                        continue;
                    }

                    if (arg.StartsWith("/"))
                    {
                        if (arg.StartsWith("/reference:") || arg.StartsWith("/r:"))
                        {
                            // list of references passed from from project file. separator =';'
                            string   refstr     = arg.Substring(arg.IndexOf(':') + 1);
                            string[] references = refstr.Split(';');
                            foreach (string rp in references)
                            {
                                AssertWellFormed(rp, true);
                                options.ReferencedAssemblies.Add(rp);
                            }
                        }
                        else if (arg.StartsWith("/in:"))
                        {
                            var infile = arg.Substring(arg.IndexOf(':') + 1);
                            AssertWellFormed(infile);
                            options.InputLib = new FileInfo(infile);
                        }
                        else if (arg.StartsWith("/bootstrap") || arg.StartsWith("/boot"))
                        {
                            // special case for building circular dependecy in preprocessing:
                            // Do not build the input assembly, assume that some other build step
                            options.CodeGenFile = Path.GetFullPath(CodeGenFileRelativePathCSharp);
                            if (GrainClientGeneratorFlags.Verbose)
                            {
                                Console.WriteLine(
                                    "Orleans-CodeGen - Set CodeGenFile={0} from bootstrap",
                                    options.CodeGenFile);
                            }
                        }
                        else if (arg.StartsWith("/sources:") || arg.StartsWith("/src:"))
                        {
                            var sourcesStr = arg.Substring(arg.IndexOf(':') + 1);

                            string[] sources = sourcesStr.Split(';');
                            foreach (var source in sources)
                            {
                                HandleSourceFile(source, options);
                            }
                        }
                    }
                    else
                    {
                        HandleSourceFile(arg, options);
                    }
                }

                if (options.TargetLanguage != Language.CSharp)
                {
                    ConsoleText.WriteLine(
                        "ERROR: Compile-time code generation is supported for C# only. "
                        + "Remove code generation from your project in order to use run-time code generation.");
                    return(2);
                }

                // STEP 2 : Validate and calculate unspecified parameters
                if (options.InputLib == null)
                {
                    Console.WriteLine("ERROR: Orleans-CodeGen - no input file specified.");
                    return(2);
                }

                if (string.IsNullOrEmpty(options.CodeGenFile))
                {
                    Console.WriteLine(
                        "ERROR: No codegen file. Add a file '{0}' to your project",
                        Path.Combine("Properties", "orleans.codegen.cs"));
                    return(2);
                }

                options.SourcesDir = Path.Combine(options.InputLib.DirectoryName, "Generated");

                // STEP 3 : Dump useful info for debugging
                Console.WriteLine(
                    "Orleans-CodeGen - Options " + Environment.NewLine + "\tInputLib={0} " + Environment.NewLine
                    + "\tCodeGenFile={1}",
                    options.InputLib.FullName,
                    options.CodeGenFile);

                if (options.ReferencedAssemblies != null)
                {
                    Console.WriteLine("Orleans-CodeGen - Using referenced libraries:");
                    foreach (string assembly in options.ReferencedAssemblies)
                    {
                        Console.WriteLine("\t{0} => {1}", Path.GetFileName(assembly), assembly);
                    }
                }

                // STEP 5 : Finally call code generation
                if (!CreateGrainClientAssembly(options))
                {
                    return(-1);
                }

                // DONE!
                return(0);
            }
            catch (Exception ex)
            {
                File.WriteAllText("error.txt", ex.Message + Environment.NewLine + ex.StackTrace);
                Console.WriteLine("-- Code-gen FAILED -- \n{0}", TraceLogger.PrintException(ex));
                return(3);
            }
        }
Example #11
0
        private static void InitializeImpl_FromFile(FileInfo configFile)
        {
            if (GrainClient.IsInitialized)
            {
                Trace.TraceInformation("Connection to Orleans gateway silo already initialized.");
                return;
            }

            ClientConfiguration config;

            try
            {
                if (configFile == null)
                {
                    Trace.TraceInformation("Looking for standard Orleans client config file");
                    config = ClientConfiguration.StandardLoad();
                }
                else
                {
                    var configFileLocation = configFile.FullName;
                    Trace.TraceInformation("Loading Orleans client config file {0}", configFileLocation);
                    config = ClientConfiguration.LoadFromFile(configFileLocation);
                }
            }
            catch (Exception ex)
            {
                var msg = String.Format("Error loading Orleans client configuration file {0} {1} -- unable to continue. {2}", configFile, ex.Message, TraceLogger.PrintException(ex));
                Trace.TraceError(msg);
                throw new AggregateException(msg, ex);
            }

            Trace.TraceInformation("Overriding Orleans client config from Azure runtime environment.");
            try
            {
                config.DeploymentId         = GetDeploymentId();
                config.DataConnectionString = GetDataConnectionString();
                config.GatewayProvider      = ClientConfiguration.GatewayProviderType.AzureTable;
            }
            catch (RoleEnvironmentException ex)
            {
                var msg = string.Format("ERROR: No AzureClient role setting value '{0}' specified for this role -- unable to continue", AzureConstants.DataConnectionConfigurationSettingName);
                Trace.TraceError(msg);
                throw new AggregateException(msg, ex);
            }

            InitializeImpl_FromConfig(config);
        }
Example #12
0
        /// <summary>
        /// Generates and loads code for the specified inputs.
        /// </summary>
        /// <param name="inputs">The assemblies to generate code for.</param>
        public void GenerateAndLoadForAssemblies(params Assembly[] inputs)
        {
            if (inputs == null)
            {
                throw new ArgumentNullException("inputs");
            }

            var timer = Stopwatch.StartNew();

            foreach (var input in inputs)
            {
                RegisterGeneratedCodeTargets(input);
                TryLoadGeneratedAssemblyFromCache(input);
            }

            var grainAssemblies = inputs.Where(ShouldGenerateCodeForAssembly).ToList();

            if (grainAssemblies.Count == 0)
            {
                // Already up to date.
                return;
            }

            try
            {
                // Generate code for newly loaded assemblies.
                var generatedSyntax = GenerateForAssemblies(grainAssemblies, true);

                var compiled = default(byte[]);
                if (generatedSyntax.Syntax != null)
                {
                    compiled = CompileAndLoad(generatedSyntax);
                }

                foreach (var assembly in generatedSyntax.SourceAssemblies)
                {
                    var generatedAssembly = new GeneratedAssembly {
                        Loaded = true, RawBytes = compiled
                    };
                    CompiledAssemblies.AddOrUpdate(
                        assembly.GetName().FullName,
                        generatedAssembly,
                        (_, __) => generatedAssembly);
                }

                if (Logger.IsVerbose2)
                {
                    Logger.Verbose2(
                        (int)ErrorCode.CodeGenCompilationSucceeded,
                        "Generated code for {0} assemblies in {1}ms",
                        generatedSyntax.SourceAssemblies.Count,
                        timer.ElapsedMilliseconds);
                }
            }
            catch (Exception exception)
            {
                var message = string.Format(
                    "Exception generating code for input assemblies:\n{0}\nException: {1}",
                    string.Join("\n", grainAssemblies.Select(_ => _.GetName().FullName)),
                    TraceLogger.PrintException(exception));
                Logger.Warn((int)ErrorCode.CodeGenCompilationFailed, message, exception);
                throw;
            }
        }
Example #13
0
        public int RunMain(string[] args)
        {
            ConsoleText.WriteStatus("Orleans-CodeGen - command-line = {0}", Environment.CommandLine);

            if (args.Length < 1)
            {
                Console.WriteLine(
                    "Usage: ClientGenerator.exe <grain interface dll path> [<client dll path>] [<key file>] [<referenced assemblies>]");
                Console.WriteLine(
                    "       ClientGenerator.exe /server <grain dll path> [<factory dll path>] [<key file>] [<referenced assemblies>]");
                return(1);
            }

            try
            {
                var options = new CodeGenOptions();

                // STEP 1 : Parse parameters
                if (args.Length == 1 && args[0].StartsWith("@"))
                {
                    // Read command line args from file
                    string arg      = args[0];
                    string argsFile = arg.Trim('"').Substring(1).Trim('"');
                    Console.WriteLine("Orleans-CodeGen - Reading code-gen params from file={0}", argsFile);
                    AssertWellFormed(argsFile, true);
                    args = File.ReadAllLines(argsFile);
                }
                int i = 1;
                foreach (string a in args)
                {
                    string arg = a.Trim('"').Trim().Trim('"');
                    if (GrainClientGeneratorFlags.Verbose)
                    {
                        Console.WriteLine("Orleans-CodeGen - arg #{0}={1}", i++, arg);
                    }
                    if (string.IsNullOrEmpty(arg) || string.IsNullOrWhiteSpace(arg))
                    {
                        continue;
                    }

                    if (arg.StartsWith("/"))
                    {
                        if (arg.StartsWith("/reference:") || arg.StartsWith("/r:"))
                        {
                            // list of references passed from from project file. separator =';'
                            string   refstr     = arg.Substring(arg.IndexOf(':') + 1);
                            string[] references = refstr.Split(';');
                            foreach (string rp in references)
                            {
                                AssertWellFormed(rp, true);
                                options.ReferencedAssemblies.Add(rp);
                            }
                        }
                        else if (arg.StartsWith("/cwd:"))
                        {
                            options.WorkingDirectory = arg.Substring(arg.IndexOf(':') + 1);
                        }
                        else if (arg.StartsWith("/in:"))
                        {
                            var infile = arg.Substring(arg.IndexOf(':') + 1);
                            AssertWellFormed(infile);
                            options.InputLib = new FileInfo(infile);
                        }
                        else if (arg.StartsWith("/keyfile:") || arg.StartsWith("/key:"))
                        {
                            string keyFile = arg.Substring(arg.IndexOf(':') + 1);
                            if (!string.IsNullOrWhiteSpace(keyFile))
                            {
                                AssertWellFormed(keyFile, true);
                                options.SigningKey = new FileInfo(keyFile);
                            }
                        }
                        else if (arg.StartsWith("/config:"))
                        {
                            options.Config = arg.Substring(arg.IndexOf(':') + 1);
                        }
                        else if (arg.StartsWith("/fsharp:"))
                        {
                            var path = arg.Substring(arg.IndexOf(':') + 1);
                            if (!string.IsNullOrEmpty(path))
                            {
                                Console.WriteLine("F# compiler path = '{0}' ", path);
                                options.FSharpCompilerPath = path;
                            }
                            else
                            {
                                Console.WriteLine("F# compiler path not set.");
                            }
                        }
                        else if (arg.StartsWith("/rootns:") || arg.StartsWith("/rns:"))
                        {
                            options.RootNamespace = arg.Substring(arg.IndexOf(':') + 1);
                        }
                        else if (arg.StartsWith("/bootstrap") || arg.StartsWith("/boot"))
                        {
                            // special case for building circular dependecy in preprocessing:
                            // Do not build the input assembly, assume that some other build step
                            options.CodeGenFile = Path.GetFullPath(CodeGenFileRelativePathCSharp);
                            if (GrainClientGeneratorFlags.Verbose)
                            {
                                Console.WriteLine(
                                    "Orleans-CodeGen - Set CodeGenFile={0} from bootstrap",
                                    options.CodeGenFile);
                            }
                        }
                        else if (arg.StartsWith("/define:") || arg.StartsWith("/d:"))
                        {
                            // #define constants passed from project file. separator =';'
                            var definsStr = arg.Substring(arg.IndexOf(':') + 1);

                            if (!string.IsNullOrWhiteSpace(definsStr))
                            {
                                string[] defines = definsStr.Split(';');
                                foreach (var define in defines)
                                {
                                    options.Defines.Add(define);
                                }
                            }
                        }
                        else if (arg.StartsWith("/imports:") || arg.StartsWith("/i:"))
                        {
                            // Standard VB imports passed from project file. separator =';'
                            string importsStr = arg.Substring(arg.IndexOf(':') + 1);

                            if (!string.IsNullOrWhiteSpace(importsStr))
                            {
                                string[] imports = importsStr.Split(';');
                                foreach (var import in imports)
                                {
                                    options.Imports.Add(import);
                                }
                            }
                        }
                        else if (arg.StartsWith("/Option")) // VB-specific options
                        {
                            if (arg.StartsWith("/OptionExplicit:"))
                            {
                                options.OptionExplicit = arg.Substring(arg.IndexOf(':') + 1);
                            }
                            else if (arg.StartsWith("/OptionStrict:"))
                            {
                                options.OptionStrict = arg.Substring(arg.IndexOf(':') + 1);
                            }
                            else if (arg.StartsWith("/OptionInfer:"))
                            {
                                options.OptionInfer = arg.Substring(arg.IndexOf(':') + 1);
                            }
                            else if (arg.StartsWith("/OptionCompare:"))
                            {
                                options.OptionCompare = arg.Substring(arg.IndexOf(':') + 1);
                            }
                        }
                        else if (arg.StartsWith("/MyType:")) // VB-specific option
                        {
                            options.MyType = arg.Substring(arg.IndexOf(':') + 1);
                        }
                        else if (arg.StartsWith("/sources:") || arg.StartsWith("/src:"))
                        {
                            // C# sources passed from from project file. separator = ';'
                            //if (GrainClientGeneratorFlags.Verbose)
                            //    Console.WriteLine("Orleans-CodeGen - Unpacking source file list arg={0}", arg);

                            var sourcesStr = arg.Substring(arg.IndexOf(':') + 1);
                            //if (GrainClientGeneratorFlags.Verbose)
                            //    Console.WriteLine("Orleans-CodeGen - Splitting source file list={0}", sourcesStr);

                            string[] sources = sourcesStr.Split(';');
                            foreach (var source in sources)
                            {
                                AddSourceFile(
                                    options.SourceFiles,
                                    ref options.LanguageConflict,
                                    ref options.TargetLanguage,
                                    ref options.CodeGenFile,
                                    source);
                            }
                        }
                    }
                    else
                    {
                        // files passed in without associated flags , we'll make the best guess.
                        if (arg.ToLowerInvariant().EndsWith(".snk", StringComparison.InvariantCultureIgnoreCase))
                        {
                            options.SigningKey = new FileInfo(arg);
                        }
                        else
                        {
                            AddSourceFile(
                                options.SourceFiles,
                                ref options.LanguageConflict,
                                ref options.TargetLanguage,
                                ref options.CodeGenFile,
                                arg);
                        }
                    }
                }

                if (!options.TargetLanguage.HasValue)
                {
                    Console.WriteLine("ERROR: Unable to determine source code language to use for code generation.");
                    return(2);
                }

                // STEP 2 : Validate and calculate unspecified parameters
                if (options.InputLib == null)
                {
                    Console.WriteLine("ERROR: Orleans-CodeGen - no input file specified.");
                    return(2);
                }

                if (string.IsNullOrEmpty(options.CodeGenFile))
                {
                    Console.WriteLine(
                        "ERROR: No codegen file. Add a file '{0}' to your project",
                        Path.Combine("Properties", "orleans.codegen.cs"));
                    return(2);
                }

                // STEP 3 :  Check timestamps and skip if output is up-to-date wrt to all inputs

/*                if (!bootstrap && IsProjectUpToDate(options.InputLib, options.SourceFiles, options.ReferencedAssemblies)
 *                  && !Debugger.IsAttached)
 *              {
 *                  Console.WriteLine(
 *                      "Orleans-CodeGen - Skipping because all output files are up-to-date with respect to the input files.");
 *                  return 0;
 *              }*/

                options.SourcesDir = Path.Combine(options.InputLib.DirectoryName, "Generated");

                // STEP 4 : Dump useful info for debugging
                Console.WriteLine(
                    "Orleans-CodeGen - Options " + Environment.NewLine + "\tInputLib={0} " + Environment.NewLine
                    + "\tSigningKey={1} " + Environment.NewLine
                    + "\tCodeGenFile={2}",
                    options.InputLib.FullName,
                    options.SigningKey != null ? options.SigningKey.FullName : "",
                    options.CodeGenFile);

                if (options.ReferencedAssemblies != null)
                {
                    Console.WriteLine("Orleans-CodeGen - Using referenced libraries:");
                    foreach (string assembly in options.ReferencedAssemblies)
                    {
                        Console.WriteLine("\t{0} => {1}", Path.GetFileName(assembly), assembly);
                    }
                }

                // STEP 5 :
//                if (!bootstrap) BuildInputAssembly(options);

                // STEP 6 : Finally call code generation
                if (!CreateGrainClientAssembly(options))
                {
                    return(-1);
                }

                // DONE!
                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("-- Code-gen FAILED -- \n{0}", TraceLogger.PrintException(ex));
                return(3);
            }
        }
Example #14
0
        protected override void ProcessBatch(List <Message> msgs)
        {
            if (Log.IsVerbose2)
            {
                Log.Verbose2("Got {0} messages to send.", msgs.Count);
            }
            for (int i = 0; i < msgs.Count;)
            {
                bool sendThisMessage = PrepareMessageForSend(msgs[i]);
                if (sendThisMessage)
                {
                    i++;
                }
                else
                {
                    msgs.RemoveAt(i); // don't advance i
                }
            }

            if (msgs.Count <= 0)
            {
                return;
            }

            Socket      sock;
            string      error;
            SiloAddress targetSilo;
            bool        continueSend = GetSendingSocket(msgs[0], out sock, out targetSilo, out error);

            if (!continueSend)
            {
                foreach (Message msg in msgs)
                {
                    OnGetSendingSocketFailure(msg, error);
                }

                return;
            }

            List <ArraySegment <byte> > data;
            int headerLength = 0;

            continueSend = SerializeMessages(msgs, out data, out headerLength, OnMessageSerializationFailure);
            if (!continueSend)
            {
                return;
            }

            int    length               = data.Sum(x => x.Count);
            int    bytesSent            = 0;
            bool   exceptionSending     = false;
            bool   countMismatchSending = false;
            string sendErrorStr         = null;

            try
            {
                bytesSent = sock.Send(data);
                if (bytesSent != length)
                {
                    // The complete message wasn't sent, even though no error was reported; treat this as an error
                    countMismatchSending = true;
                    sendErrorStr         = String.Format("Byte count mismatch on sending to {0}: sent {1}, expected {2}", targetSilo, bytesSent, length);
                    Log.Warn(ErrorCode.Messaging_CountMismatchSending, sendErrorStr);
                }
            }
            catch (Exception exc)
            {
                exceptionSending = true;
                if (!(exc is ObjectDisposedException))
                {
                    sendErrorStr = String.Format("Exception sending message to {0}. {1}", targetSilo, TraceLogger.PrintException(exc));
                    Log.Warn(ErrorCode.Messaging_ExceptionSending, sendErrorStr, exc);
                }
            }
            MessagingStatisticsGroup.OnMessageBatchSend(targetSilo, msgs[0].Direction, bytesSent, headerLength, GetSocketDirection(), msgs.Count);
            bool sendError = exceptionSending || countMismatchSending;

            if (sendError)
            {
                OnSendFailure(sock, targetSilo);
            }

            foreach (Message msg in msgs)
            {
                ProcessMessageAfterSend(msg, sendError, sendErrorStr);
            }
        }
Example #15
0
        private string MakeErrorMsg(string what, GrainReference grainReference, Exception exc)
        {
            string errorCode = AzureStorageUtils.ExtractRestErrorCode(exc);

            return(string.Format("Error from storage provider during {0} for grain Type={1} Pk={2} Id={3} Error={4}\n {5}",
                                 what, grainTypeName, grainReference.GrainId.ToDetailedString(), grainReference, errorCode, TraceLogger.PrintException(exc)));
        }
Example #16
0
        private static void InitializeImpl_FromConfig(ClientConfiguration config)
        {
            if (GrainClient.IsInitialized)
            {
                Trace.TraceInformation("Connection to Orleans gateway silo already initialized.");
                return;
            }

            //// Find endpoint info for the gateway to this Orleans silo cluster
            //Trace.WriteLine("Searching for Orleans gateway silo via Orleans instance table...");
            var deploymentId     = config.DeploymentId;
            var connectionString = config.DataConnectionString;

            if (String.IsNullOrEmpty(deploymentId))
            {
                throw new ArgumentException("Cannot connect to Azure silos with null deploymentId", "config.DeploymentId");
            }

            if (String.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentException("Cannot connect to Azure silos with null connectionString", "config.DataConnectionString");
            }

            bool      initSucceeded = false;
            Exception lastException = null;

            for (int i = 0; i < MaxRetries; i++)
            {
                try
                {
                    // Initialize will throw if cannot find Gateways
                    GrainClient.Initialize(config);
                    initSucceeded = true;
                    break;
                }
                catch (Exception exc)
                {
                    lastException = exc;
                    Trace.TraceError("Client.Initialize failed with exc -- {0}. Will try again", exc.Message);
                }
                // Pause to let Primary silo start up and register
                Trace.TraceInformation("Pausing {0} awaiting silo and gateways registration for Deployment={1}", StartupRetryPause, deploymentId);
                Thread.Sleep(StartupRetryPause);
            }

            if (initSucceeded)
            {
                return;
            }

            OrleansException err;

            err = lastException != null ? new OrleansException(String.Format("Could not Initialize Client for DeploymentId={0}. Last exception={1}",
                                                                             deploymentId, lastException.Message), lastException) : new OrleansException(String.Format("Could not Initialize Client for DeploymentId={0}.", deploymentId));
            Trace.TraceError("Error starting Orleans Azure client application -- {0} -- bailing. {1}", err.Message, TraceLogger.PrintException(err));
            throw err;
        }
Example #17
0
        private string MakeErrorMsg(string what, Exception exc)
        {
            var    httpStatusCode = HttpStatusCode.Unused;
            string errorCode      = String.Empty;

            var decoder = store as IRestExceptionDecoder;

            if (decoder != null)
            {
                decoder.DecodeException(exc, out httpStatusCode, out errorCode, true);
            }

            GrainReference grainReference = baseGrain.GrainReference;

            return(string.Format("Error from storage provider during {0} for grain Type={1} Pk={2} Id={3} Error={4}" + Environment.NewLine + " {5}",
                                 what, grainTypeName, grainReference.GrainId.ToDetailedString(), grainReference, errorCode, TraceLogger.PrintException(exc)));
        }
Example #18
0
 private string MakeErrorMsg(string what, GrainReference grainReference, Exception exc)
 {
     HttpStatusCode httpStatusCode;
     string errorCode;
     AzureStorageUtils.EvaluateException(exc, out httpStatusCode, out errorCode, true);
     return string.Format("Error from storage provider during {0} for grain Type={1} Pk={2} Id={3} Error={4}"  + Environment.NewLine + " {5}",
         what, grainTypeName, grainReference.GrainId.ToDetailedString(), grainReference, errorCode, TraceLogger.PrintException(exc));
 }