示例#1
0
        public async Task <string> ExecuteCommandAsync(string input, bool allowAdminCommands)
        {
            input.Validate("input");

            traceType.WriteInfo("ExecuteCommandAsync: {0}, allow admin commands: {1}", input, allowAdminCommands);

            // Command format is "CommandName:Arguments"
            string[] tokens      = input.Split(CommandTokenDelimiter, 2);
            string   commandName = tokens[0];

            string args = string.Empty;

            if (tokens.Length > 1)
            {
                args = tokens[1];
            }

            try
            {
                CommandHandlerEntry handlerEntry;
                if (Commands.TryGetValue(commandName, out handlerEntry))
                {
                    if (allowAdminCommands || !handlerEntry.IsAdminCommand)
                    {
                        string result = await handlerEntry.CommandHandlerAsync(args).ConfigureAwait(false);

                        return(result);
                    }

                    string text = string.Format(CultureInfo.InvariantCulture, "Permission denied for command: {0}", commandName);
                    traceType.WriteWarning(text);
                    throw new ArgumentException(text, commandName);
                }

                string text2 = string.Format(CultureInfo.InvariantCulture, "Unrecognized command: {0}", commandName);
                traceType.WriteWarning(text2);
                throw new ArgumentException(text2, commandName);
            }
            catch (Exception e)
            {
                traceType.WriteWarning("Command handler for {0} threw exception: {1}", commandName, e);
                throw;
            }
        }
        private static async Task <IVersionedKeyValue> ReadPropertyAsync(
            TraceType traceType,
            IVersionedPropertyStore versionedPropertyStore,
            Guid activityId)
        {
            try
            {
                var versionedKeyValue = await versionedPropertyStore
                                        .GetValueAsync(activityId, PolicyPropertyName, PolicyVersionName, false)
                                        .ConfigureAwait(false);

                return(versionedKeyValue);
            }
            catch (FabricElementNotFoundException ex)
            {
                traceType.WriteInfo(
                    "Properties '{0}' and '{1}' not found. These need to be created. Exception: {2}",
                    PolicyPropertyName,
                    PolicyVersionName,
                    ex);
            }

            return(null);
        }
示例#3
0
        public async Task <JobImpactTranslationMode> EvaluateJobImpactAsync(IManagementNotificationContext notification)
        {
            notification.Validate("notification");

            if (notification.NotificationType != NotificationType.StartJobStep)
            {
                // this is a coding error
                throw new ArgumentException("Notification not relevant. Notification: {0}".ToString(notification.ToShortDisplayString()));
            }

            if (DoesAssessmentMatchNotification(notification))
            {
                return(currentJobImpactData.AssessedImpact);
            }

            traceType.WriteInfo(
                "JobImpactData doesn't match, starting new evaluation. Notification: {0}, Current JobImpactData: {1}",
                notification.ToShortDisplayString(), currentJobImpactData != null ? currentJobImpactData.ToString() : "<null>");

            var now = DateTimeOffset.UtcNow;

            // There is already retry built into the QueryClient wrapper. If it goes beyond retry boundaries,
            // we'll let the caller handle this
            IList <INode> nodeList = await QueryClient.GetNodeListAsync(now).ConfigureAwait(false);

            var queriedNodes = nodeList.ToDictionary(e => e.NodeName, StringComparer.OrdinalIgnoreCase);

            Dictionary <string, INode> nodesToBeImpacted = GetNodesInNotification(notification, queriedNodes);

            var newJobImpactData = new JobImpactData
            {
                JobId          = notification.ActiveJobId,
                JobType        = notification.ActiveJobType,
                UD             = notification.ActiveJobStepTargetUD,
                AssessedNodes  = nodesToBeImpacted,
                Timestamp      = now,
                AssessedImpact = JobImpactTranslationMode.Default,
            };

            // no previous data
            if (currentJobImpactData == null)
            {
                currentJobImpactData = newJobImpactData;

                traceType.WriteInfo(
                    "New assessed job impact stored. Returning {0}, JobImpactData: {1}, Notification: {2}",
                    currentJobImpactData.AssessedImpact,
                    currentJobImpactData,
                    notification.ToShortDisplayString());

                return(currentJobImpactData.AssessedImpact);
            }

            // has too much time passed after assessment?
            bool expired = HasPreviousEvaluationExpired(now, currentJobImpactData);

            if (expired)
            {
                currentJobImpactData = newJobImpactData;

                traceType.WriteWarning(
                    "New assessed job impact stored. Time since last assessment is either invalid or has exceeded expiration time. Returning {0}. JobImpactData: {1}, Notification: {2}",
                    currentJobImpactData.AssessedImpact,
                    currentJobImpactData,
                    notification.ToShortDisplayString());

                return(currentJobImpactData.AssessedImpact);
            }

            bool?restarted = DidPreviouslyAssessedNodesRestart(queriedNodes, currentJobImpactData);

            if (restarted == null)
            {
                traceType.WriteInfo(
                    "Unable to assess job impact, continuing to use previous assessment. Returning {0}, JobImpactData: {1}, Notification: {2}",
                    currentJobImpactData.AssessedImpact,
                    currentJobImpactData,
                    notification.ToShortDisplayString());

                return(currentJobImpactData.AssessedImpact);
            }

            currentJobImpactData = newJobImpactData;

            currentJobImpactData.AssessedImpact = restarted.Value
                ? JobImpactTranslationMode.Default
                : JobImpactTranslationMode.Optimized;

            traceType.WriteInfo(
                "New assessed job impact stored. Returning {0}, JobImpactData: {1}, Notification: {2}",
                currentJobImpactData.AssessedImpact,
                currentJobImpactData,
                notification.ToShortDisplayString());

            return(currentJobImpactData.AssessedImpact);
        }
        public async Task <IVersionedKeyValue> GetValueAsync(Guid activityId, string keyName, string versionKeyName, bool useCachedValue = true)
        {
            keyName.Validate("keyName");
            versionKeyName.Validate("versionKeyName");

            if (useCachedValue)
            {
                lock (locker)
                {
                    if (cachedProperties.ContainsKey(keyName))
                    {
                        var vkv = cachedProperties[keyName];
                        return(vkv);
                    }
                }
            }

            traceType.WriteInfo(
                "Getting value from remote store{0}. keyName: {1}, versionKeyName: {2}",
                useCachedValue ? " since it doesn't exist in cache" : string.Empty,
                keyName,
                versionKeyName);

            var versionedKeyValue = await ReadPropertiesFromRemoteStoreAsync(activityId, keyName, versionKeyName).ConfigureAwait(false);

            lock (locker)
            {
                versionedKeyValue = UpdateCache(activityId, versionedKeyValue);
            }

            return(versionedKeyValue);
        }