internal void AddOrUpdateRecord(string tableKey, ServicePackageTableRecord record)
 {
     lock (this.table)
     {
         record.LatestNotificationTimestamp = DateTime.UtcNow;
         this.table[tableKey] = record;
     }
 }
        private void ProcessTableRecord(string tableRecord, string backupFilePath, EtwEventTimestamp dataTimestamp)
        {
            string[] tableRecordParts = tableRecord.Split(',');
            if (tableRecordParts.Length != (int)TableRecordParts.Count)
            {
                Utility.TraceSource.WriteError(
                    TraceType,
                    "Table record {0} in backup file {1} is not in the correct format.",
                    tableRecord,
                    backupFilePath);
                return;
            }

            string nodeName              = tableRecordParts[(int)TableRecordParts.NodeName].Trim();
            string appInstanceId         = tableRecordParts[(int)TableRecordParts.ApplicationInstanceId].Trim();
            string appRolloutVersion     = tableRecordParts[(int)TableRecordParts.ApplicationRolloutVersion].Trim();
            string servicePackageName    = tableRecordParts[(int)TableRecordParts.ServicePackageName].Trim();
            string serviceRolloutVersion = tableRecordParts[(int)TableRecordParts.ServiceRolloutVersion].Trim();
            string runLayoutRoot         = tableRecordParts[(int)TableRecordParts.RunLayoutRoot].Trim();

            ServicePackageTableRecord record = new ServicePackageTableRecord(
                nodeName,
                appInstanceId,
                appRolloutVersion,
                servicePackageName,
                serviceRolloutVersion,
                runLayoutRoot,
                default(DateTime));

            this.addOrUpdateServicePackageHandler(
                nodeName,
                appInstanceId,
                DateTime.MaxValue, // service package activation time unknown
                servicePackageName,
                record,
                dataTimestamp,
                false);
        }
        private void ProcessServicePackageActiveEvent(EventRecord eventRecord, string taskName, string eventName, EtwEventTimestamp eventTimestamp, int maxVersion)
        {
            ApplicationDataReader reader = new ApplicationDataReader(
                eventRecord.UserData,
                eventRecord.UserDataLength);

            // Verify event version
            int eventVersion = reader.ReadInt32();

            if (eventVersion > maxVersion)
            {
                Utility.TraceSource.WriteError(
                    TraceType,
                    "Unexpected version {0} encountered for event name {1}. Event will be ignored.",
                    eventVersion,
                    eventName);
                return;
            }

            // Get the node name
            string nodeName = reader.ReadUnicodeString();

            // Get the root directory for the run layout
            string runLayoutRoot = reader.ReadUnicodeString();

            // Get the application instance ID
            string appInstanceId = reader.ReadUnicodeString();

            // Get the application rollout version
            string appRolloutVersion = reader.ReadUnicodeString();

            // Get the service package name
            string servicePackageName = reader.ReadUnicodeString();

            // Get the service package rollout version
            string serviceRolloutVersion = reader.ReadUnicodeString();

            // Create a record for the application data table
            ServicePackageTableRecord record = new ServicePackageTableRecord(
                nodeName,
                appInstanceId,
                appRolloutVersion,
                servicePackageName,
                serviceRolloutVersion,
                runLayoutRoot,
                default(DateTime));

            Utility.TraceSource.WriteInfo(
                TraceType,
                "ETW event received. Task {0}, event {1}, node name: {2}, application instance ID: {3}, application rollout version: {4}, service package name: {5}, service rollout version: {6}.",
                taskName,
                eventName,
                nodeName,
                record.ApplicationInstanceId,
                record.ApplicationRolloutVersion,
                record.ServicePackageName,
                record.ServiceRolloutVersion);

            // Add or update record in service package table
            this.AddOrUpdateServicePackageHandler(
                nodeName,
                appInstanceId,
                eventTimestamp.Timestamp,
                servicePackageName,
                record,
                eventTimestamp,
                true);
        }
Esempio n. 4
0
        private void RemoveServicePackage(
            string nodeName,
            string applicationInstanceId,
            string servicePackageName,
            EtwEventTimestamp dataTimestamp,
            bool updateBackupFile,
            bool removeDueToInactivity)
        {
            try
            {
                // Check if the service package already exists in the service package table
                string uniqueAppId     = string.Concat(nodeName, applicationInstanceId);
                string uniqueServiceId = string.Concat(uniqueAppId, servicePackageName);
                List <ServicePackageTableRecord> records = this.servicePackageTable.GetRecordsForAppInstance(
                    nodeName,
                    applicationInstanceId);
                ServicePackageTableRecord record = (null == records) ?
                                                   null :
                                                   records.FirstOrDefault(s => s.ServicePackageName.Equals(
                                                                              servicePackageName,
                                                                              StringComparison.Ordinal));
                if (null != record)
                {
                    // Service package exists in the service package table
                    bool skipRemove = false;

                    if (removeDueToInactivity)
                    {
                        // We are removing this service package not because it was explicitly
                        // deactivated by hosting, but because we recently did not receive any
                        // notification from hosting that this service package is still active.
                        // However, if some activity occurred on the service package since our
                        // last check, then don't remove this service package.
                        DateTime cutoffTime = DateTime.UtcNow.AddSeconds(-1 * this.maxServicePackageInactiveTimeSeconds);
                        if (record.LatestNotificationTimestamp.CompareTo(cutoffTime) >= 0)
                        {
                            Utility.TraceSource.WriteInfo(
                                TraceType,
                                "Skipping removal of entry from service package table because recent activity was detected. Node name: {0}, application instance ID: {1}, service package name: {2}.",
                                nodeName,
                                applicationInstanceId,
                                servicePackageName);
                            skipRemove = true;
                        }
                    }

                    if (false == skipRemove)
                    {
                        if (records.Count > 1)
                        {
                            // There are other service packages for this application instance
                            this.applicationInstanceManager.RemoveServiceFromApplicationInstance(uniqueAppId, servicePackageName);
                        }
                        else
                        {
                            // This is the last service package for its application instance
                            // Delete the data collector for this application instance
                            this.applicationInstanceManager.DeleteApplicationInstance(
                                uniqueAppId);
                        }

                        // Delete the service package information from our table
                        this.servicePackageTable.RemoveRecord(uniqueServiceId);

                        Utility.TraceSource.WriteInfo(
                            TraceType,
                            "Entry removed from service package table: Node name: {0}, application instance ID: {1}, service package name: {2}.",
                            nodeName,
                            applicationInstanceId,
                            servicePackageName);
                    }
                }
                else
                {
                    Utility.TraceSource.WriteInfo(
                        TraceType,
                        "Deactivation of unknown service package ignored. Node name: {0}, application instance ID: {1}, service package name: {2}.",
                        nodeName,
                        applicationInstanceId,
                        servicePackageName);
                }
            }
            finally
            {
                // If necessary, update the backup file on disk to reflect this change
                if (updateBackupFile)
                {
                    if (false == this.tableBackup.Update(
                            this.servicePackageTable.GetAllRecords(),
                            dataTimestamp))
                    {
                        Utility.TraceSource.WriteError(
                            TraceType,
                            "Unable to make backup of service package table to a file on disk.");
                    }
                }
            }
        }
Esempio n. 5
0
        private void AddOrUpdateServicePackage(
            string nodeName,
            string applicationInstanceId,
            DateTime servicePackageActivationTime,
            string servicePackageName,
            ServicePackageTableRecord serviceRecord,
            EtwEventTimestamp dataTimestamp,
            bool updateBackupFile)
        {
            try
            {
                // Create the object that wraps the application configuration
                AppConfig appConfig;
                try
                {
                    appConfig = new AppConfig(
                        nodeName,
                        serviceRecord.RunLayoutRoot,
                        applicationInstanceId,
                        serviceRecord.ApplicationRolloutVersion);
                }
                catch (InvalidOperationException)
                {
                    return;
                }

                // Create the object that wraps the service configuration
                ServiceConfig serviceConfig;
                try
                {
                    serviceConfig = new ServiceConfig(
                        serviceRecord.RunLayoutRoot,
                        applicationInstanceId,
                        servicePackageName,
                        serviceRecord.ServiceRolloutVersion);
                }
                catch (InvalidOperationException)
                {
                    return;
                }

                // Check if the service package already exists in the service package table
                string uniqueAppId     = string.Concat(nodeName, applicationInstanceId);
                string uniqueServiceId = string.Concat(uniqueAppId, servicePackageName);
                List <ServicePackageTableRecord> records = this.servicePackageTable.GetRecordsForAppInstance(
                    nodeName,
                    applicationInstanceId);
                ServicePackageTableRecord record = (null == records) ?
                                                   null :
                                                   records.FirstOrDefault(s => s.ServicePackageName.Equals(
                                                                              servicePackageName,
                                                                              StringComparison.Ordinal));
                if (null != record)
                {
                    // Service package already exists. Check if needs to be updated.
                    if ((false == record.ApplicationRolloutVersion.Equals(serviceRecord.ApplicationRolloutVersion)) ||
                        (false == record.ServiceRolloutVersion.Equals(serviceRecord.ServiceRolloutVersion)))
                    {
                        this.applicationInstanceManager.AddServiceToApplicationInstance(
                            uniqueAppId,
                            appConfig,
                            servicePackageName,
                            serviceConfig);
                    }
                    else
                    {
                        Utility.TraceSource.WriteInfo(
                            TraceType,
                            "Service package activation did not require the data collector to be restarted. Node name: {0}, application instance ID: {1}, app rollout version: {2}, service package name: {3}, service rollout version: {4}.",
                            nodeName,
                            applicationInstanceId,
                            serviceRecord.ApplicationRolloutVersion,
                            servicePackageName,
                            serviceRecord.ServiceRolloutVersion);
                    }
                }
                else
                {
                    // Service package does not exist yet. Check whether any other service packages
                    // exist for the same app instance
                    if (records.Count > 0)
                    {
                        // Other service packages exist for this application instance.
                        this.applicationInstanceManager.AddServiceToApplicationInstance(
                            uniqueAppId,
                            appConfig,
                            servicePackageName,
                            serviceConfig);
                    }
                    else
                    {
                        // No service packages exist for this application instance.
                        // Create a new data collector for this application instance.
                        this.applicationInstanceManager.CreateApplicationInstance(
                            uniqueAppId,
                            servicePackageActivationTime,
                            appConfig,
                            servicePackageName,
                            serviceConfig);
                    }
                }

                // Store information about this service package in our table
                this.servicePackageTable.AddOrUpdateRecord(uniqueServiceId, serviceRecord);
            }
            finally
            {
                // If necessary, update the backup file on disk to reflect this change
                if (updateBackupFile)
                {
                    if (false == this.tableBackup.Update(
                            this.servicePackageTable.GetAllRecords(),
                            dataTimestamp))
                    {
                        Utility.TraceSource.WriteError(
                            TraceType,
                            "Unable to make backup of service package table to a file on disk.");
                    }
                }
            }

            Utility.TraceSource.WriteInfo(
                TraceType,
                "Entry added to/updated in service package table: Node name: {0}, application instance ID: {1}, app rollout version: {2}, service package name: {3}, service rollout version: {4}.",
                nodeName,
                applicationInstanceId,
                serviceRecord.ApplicationRolloutVersion,
                servicePackageName,
                serviceRecord.ServiceRolloutVersion);
        }