コード例 #1
0
ファイル: Writer.cs プロジェクト: radtek/sqlwristband
        } // end of GenerateSlowSqlSingleRow function

        // Generates INSERT statement for a single data row - fast changing metric
        private static string GenerateSqlSingleRowFast(int targetId, MetricGroup metricGroup, ProbeResultingData data)
        {
            string dataSqlStmt = "INSERT INTO " + SqlServerProbe.DataTableName(targetId, metricGroup) + " (dt,";

            for (int i = 0; i < metricGroup.NumberOfMetrics; i++)
            {
                dataSqlStmt += metricGroup.metrics[i].name.Replace(' ', '_') + ",";
            }

            dataSqlStmt  = dataSqlStmt.Remove(dataSqlStmt.Length - 1); // remove last comma
            dataSqlStmt += ")" + Environment.NewLine + "VALUES ('" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "',";

            if (metricGroup.NumberOfMetrics != data.NumberOfColumns)
            {
                throw new Exception("Number of metrics don't match number of columns in probe results");
            }

            // add metric values
            for (int i = 0; i < metricGroup.NumberOfMetrics; i++)
            {
                dataSqlStmt += SqlServerProbe.DataValueToString(metricGroup.metrics[i].type, data.values[0, metricGroup.NumberOfMultiRowKeys + metricGroup.NumberOfMultiRowKeyAttributes + i]) + ",";
            }

            dataSqlStmt  = dataSqlStmt.Remove(dataSqlStmt.Length - 1); // remove last comma
            dataSqlStmt += ")";

            return(dataSqlStmt);
        } // end of GenerateFastSqlSingleRow function
コード例 #2
0
ファイル: Writer.cs プロジェクト: radtek/sqlwristband
        } // end of ProcessQueue function

        #endregion public methods declarations

        #region private static methods declarations

        /// <summary>Generates UPDATE or INSERT statement for a single data row - slow changing metric</summary>
        private static string GenerateSqlSingleRowSlow(int targetId, MetricGroup metricGroup, int dataMatches, ProbeResultingData data)
        {
            string dataSqlStmt;

            if (dataMatches == 0) // just update endDate when current data matches one stored in repository
            {
                dataSqlStmt = "UPDATE " + SqlServerProbe.DataTableName(targetId, metricGroup)
                              + " SET endDate = '" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "'"
                              + " WHERE startDate = (SELECT MAX(startDate) FROM " + SqlServerProbe.DataTableName(targetId, metricGroup) + ")";
            }
            else
            {
                if (metricGroup.NumberOfMetrics != data.NumberOfColumns)
                {
                    throw new Exception("Number of metrics doesn't match number of columns in probe results");
                }

                dataSqlStmt = "INSERT INTO " + SqlServerProbe.DataTableName(targetId, metricGroup) + " (";

                for (int i = 0; i < metricGroup.NumberOfMetrics; i++)
                {
                    dataSqlStmt += metricGroup.metrics[i].name.Replace(' ', '_') + ",";
                }

                dataSqlStmt += "startDate,endDate)"
                               + Environment.NewLine + "VALUES (";

                // add metric values
                for (int i = 0; i < metricGroup.NumberOfMetrics; i++)
                {
                    dataSqlStmt += SqlServerProbe.DataValueToString(metricGroup.metrics[i].type, data.values[0, metricGroup.NumberOfMultiRowKeys + metricGroup.NumberOfMultiRowKeyAttributes + i]) + ",";
                }

                // startDate,endDate
                dataSqlStmt += "'" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "','" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "')";
            }

            return(dataSqlStmt);
        } // end of GenerateSlowSqlSingleRow function
コード例 #3
0
ファイル: Purger.cs プロジェクト: radtek/sqlwristband
        // method that runs continuously and purges data in repository
        public void Work()
        {
            int              targetId;
            int              timeTableId;
            string           sqlStmt;
            MetricGroup      metricGroup;
            InstanceSchedule schedule;
            List <Tuple <DateTime, int> > lastPurgeList;

            _logger.Info("Purger started");

            _reposConn = new SqlConnection(Configuration.GetReposConnectionString("Purger"));

            while (!_shouldStop)
            {
                try
                {
                    // this is to wait until the repository is ready to serve requests
                    if (!Manager.IsRepositoryAccessible)
                    {
                        Thread.Sleep(250);
                        continue;
                    }

                    if (this._reposConn.State != ConnectionState.Open)
                    {
                        this._reposConn.Open();
                    }

                    lastPurgeList = Configuration.timeTable.LastPurge();
                    foreach (Tuple <DateTime, int> lastPurge in lastPurgeList)
                    {
                        timeTableId = lastPurge.Item2;

                        // Skip schedules that have been deleted
                        if (!Configuration.timeTable.TryGetValue(timeTableId, out schedule))
                        {
                            continue;
                        }

                        // skip schedules that were purged recently
                        if (DateTime.Compare(lastPurge.Item1.AddMinutes(Configuration.purgeInterval), DateTime.Now) > 0)
                        {
                            continue;
                        }

                        targetId    = schedule._targetId;
                        metricGroup = Configuration.metricGroups[schedule._metricGroupId];

                        // prepare SQL statement
                        switch (metricGroup.changeSpeed)
                        {
                        case ChangeSpeed.Slow:
                            sqlStmt = "DELETE FROM " + SqlServerProbe.DataTableName(targetId, metricGroup)
                                      + " WHERE endDate <= '" + SqlServerProbe.DateTimeToString(DateTime.Now.AddHours(-1 * schedule._schedule.retention)) + "'";
                            break;

                        case ChangeSpeed.Fast:
                            sqlStmt = "DELETE FROM " + SqlServerProbe.DataTableName(targetId, metricGroup)
                                      + " WHERE dt <= '" + SqlServerProbe.DateTimeToString(DateTime.Now.AddHours(-1 * schedule._schedule.retention)) + "'";
                            break;

                        default:
                            throw new Exception("Unsupported change speed");
                        }

                        // execute SQL statement
                        try
                        {
                            if (!Manager.IsRepositoryAccessible)
                            {
                                Thread.Sleep(250);
                                break;
                            }

                            if (_reposConn.State != ConnectionState.Open)
                            {
                                _reposConn.Open();
                            }

                            using (SqlCommand cmd = _reposConn.CreateCommand())
                            {
                                cmd.CommandType    = CommandType.Text;
                                cmd.CommandText    = sqlStmt;
                                cmd.CommandTimeout = 300;
                                int rowCount = cmd.ExecuteNonQuery();
                                _logger.Debug("Rows deleted from table " + SqlServerProbe.DataTableName(targetId, metricGroup) + ": " + rowCount.ToString());
                            }

                            // update last purge time
                            Configuration.timeTable.SetLastPurge(lastPurge.Item2, DateTime.Now);
                        }
                        catch (SqlException e)
                        {
                            if (e.Number == 208)
                            {
                                _logger.Debug("Table " + SqlServerProbe.DataTableName(targetId, metricGroup) + " does not exist");
                            }
                            else if (this._reposConn.State != ConnectionState.Open)
                            {
                                Manager.SetRepositoryAccessibility(false);
                            }
                            else
                            {
                                _logger.Error("Could not purge " + SqlServerProbe.DataTableName(targetId, metricGroup) + " due to error: " + e.Message);
                            }
                        }
                        catch (Exception e)
                        {
                            if (this._reposConn.State == ConnectionState.Open)
                            {
                                _logger.Error("Could not purge " + SqlServerProbe.DataTableName(targetId, metricGroup) + " due to error: " + e.Message);
                            }
                            else
                            {
                                Manager.SetRepositoryAccessibility(false);
                            }

                            continue;
                        }

                        Thread.Sleep(250); // we don't want to stress the repository too much
                    } // foreach

                    Thread.Sleep(250);
                }
                catch (Exception e)
                {
                    if (this._reposConn != null)
                    {
                        switch (this._reposConn.State)
                        {
                        case System.Data.ConnectionState.Broken:
                        case System.Data.ConnectionState.Closed:
                            Manager.SetRepositoryAccessibility(false);
                            break;

                        default:
                            _logger.Error(e.Message);
                            _logger.Error(e.StackTrace);
                            _mgr.ReportFailure("Purger");
                            return;
                        }
                    }
                    else
                    {
                        _logger.Error(e.Message);
                        _logger.Error(e.StackTrace);
                        _mgr.ReportFailure("Purger");
                        return;
                    }
                } // end of catch
            }     // end of while (!_shouldStop)

            _logger.Info("Purger stopped");
        }
コード例 #4
0
ファイル: Writer.cs プロジェクト: radtek/sqlwristband
        } // end of WriteFastSingleRowToRepository function

        /// <summary>Saves data into dictionary and data table for multi-value metrics</summary>
        private void WriteMultipleRowsToRepository(int targetId, MetricGroup metricGroup, ProbeResultingData data)
        {
            int        id;
            CacheTable dictCache, dataCache;
            List <int> newDictRows;
            List <Tuple <int, int> > oldDictRows;

            object[] key, attributes;
            string   dataTableName, dictTableName;

            byte           tryCount    = 0;
            bool           canExit     = false;
            SqlTransaction tran        = null;
            string         dictSqlStmt = string.Empty;
            string         dataSqlStmt = string.Empty;

            newDictRows = new List <int>();               // ids of records that should be added to the dictionary (new rows or rows with updated attributes)
            oldDictRows = new List <Tuple <int, int> >(); // ids and dictionary ids of records that changed since last probe and need to be closed

            dataTableName = SqlServerProbe.DataTableName(targetId, metricGroup);
            _logger.Debug("Name of data table: " + dataTableName);

            dictTableName = SqlServerProbe.DictTableName(targetId, metricGroup);
            _logger.Debug("Name of dictionary: " + dictTableName);

            // load the dictionary cache table if it doesn't exist
            if (!InMemoryCache.ContainsKey(dictTableName))
            {
                InMemoryCache.LoadDictionaryIntoCache(targetId, metricGroup, false);
            }

            dictCache = Configuration.inMemoryCache[dictTableName];

            // load the dictionary cache table if it doesn't exist
            if (!InMemoryCache.ContainsKey(dataTableName))
            {
                InMemoryCache.LoadDataIntoCache(targetId, metricGroup, false);
            }

            /*
             * Checks for changed or new records in dictionary and if needed prepares SQL statement to update dictionary table
             */
            switch (metricGroup.multiRowKeyAttributesChangeSpeed)
            {
            case ChangeSpeed.Static:
                // check whether all records are in the dictionary or some need to be added to it
                for (int i = 0; i < data.NumberOfRows; i++)
                {
                    key = new object[metricGroup.NumberOfMultiRowKeys];

                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeys; j++)
                    {
                        key[j] = data.values[i, j];
                    }

                    if (dictCache.GetIdByKey(key) == -1)
                    {
                        newDictRows.Add(i);
                    }
                }

                // generate SQL statements if there are any new dictionary records
                if (newDictRows.Count > 0)
                {
                    dictSqlStmt = GenerateSqlStaticDict(targetId, metricGroup, data, newDictRows);
                }

                break;

            case ChangeSpeed.Slow:
                // check whether all records are in the dictionary or some need to be added to it
                for (int i = 0; i < data.NumberOfRows; i++)
                {
                    key = new object[metricGroup.NumberOfMultiRowKeys];
                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeys; j++)
                    {
                        key[j] = data.values[i, j];
                    }

                    id = dictCache.GetIdByKey(key);
                    if (id == -1)
                    {
                        newDictRows.Add(i);
                    }
                    else     // check that attributes match
                    {
                        attributes = new object[metricGroup.NumberOfMultiRowKeyAttributes];
                        for (int j = 0; j < metricGroup.NumberOfMultiRowKeyAttributes; j++)
                        {
                            attributes[j] = data.values[i, metricGroup.NumberOfMultiRowKeys + j];
                        }

                        if (!dictCache.CompareAttributesForKey(id, attributes))
                        {
                            oldDictRows.Add(new Tuple <int, int>(i, id));    // this is to close the old record - UPDATE
                        }
                    }
                }

                // generate SQL statements if there are any changes or new records in dictionary
                if (oldDictRows.Count > 0 || newDictRows.Count > 0)
                {
                    dictSqlStmt = GenerateSqlSlowDict(targetId, metricGroup, data, oldDictRows, newDictRows);
                }

                break;

            default:
                throw new Exception("Unknown dictionary change speed");
            }

            /*
             * Write new data into dictionary but don't close transaction yet
             */
            if (dictSqlStmt.CompareTo(string.Empty) != 0)
            {
                _logger.Trace(dictSqlStmt);

                // If tables don't exist, will try to create them and rerun SQL statements
                while (!canExit && tryCount < 2)
                {
                    try
                    {
                        // we will write to the dictionary first and then to the data table so we need to begin a transaction
                        tran = this.reposConn.BeginTransaction();

                        if (dictSqlStmt.CompareTo(string.Empty) != 0)
                        {
                            // save dictionary changes
                            using (SqlCommand cmd = this.reposConn.CreateCommand())
                            {
                                cmd.Transaction = tran;
                                cmd.CommandType = System.Data.CommandType.Text;
                                cmd.CommandText = dictSqlStmt;
                                int rowCount = cmd.ExecuteNonQuery();
                                _logger.Debug("Rows affected: " + rowCount.ToString());
                            }
                        }

                        InMemoryCache.LoadDictionaryIntoCache(targetId, metricGroup, true, this.reposConn, tran);
                        canExit = true;
                    }
                    catch (SqlException e)
                    {
                        if (tran != null)
                        {
                            tran.Rollback();
                            tran.Dispose();
                            tran = null;
                        }

                        switch (e.Number)
                        {
                        case 208:     // Invalid object
                            // Do not create tables if target has been deleted
                            if (!Configuration.targets.ContainsKey(targetId))
                            {
                                return;
                            }

                            SqlServerProbe.CreateTablesForMetricGroup(targetId, metricGroup);
                            break;

                        default:
                            _logger.Error("SqlException: " + e.Message + " ErrorCode: " + e.Number.ToString());
                            break;
                        }
                    }

                    tryCount++;
                }
            }

            /*
             * Prepare SQL statement to save data with right references to the dictionary records
             */
            switch (metricGroup.changeSpeed)
            {
            case ChangeSpeed.Fast:
                dataSqlStmt = "INSERT INTO " + dataTableName + " (dt,dictId,";

                for (int i = 0; i < metricGroup.NumberOfMetrics; i++)
                {
                    dataSqlStmt += metricGroup.metrics[i].name.Replace(' ', '_') + ",";
                }

                dataSqlStmt  = dataSqlStmt.Remove(dataSqlStmt.Length - 1);    // remove last comma
                dataSqlStmt += ")" + Environment.NewLine + "VALUES";

                for (int i = 0; i < data.NumberOfRows; i++)
                {
                    dataSqlStmt += Environment.NewLine + "('" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "',";

                    // retrieve corresponding id from dictionary
                    key = new object[metricGroup.NumberOfMultiRowKeys];

                    for (int k = 0; k < metricGroup.NumberOfMultiRowKeys; k++)
                    {
                        key[k] = data.values[i, k];
                    }

                    id           = dictCache.GetIdByKey(key);
                    dataSqlStmt += id.ToString() + ",";

                    // add metric values
                    for (int j = 0; j < metricGroup.NumberOfMetrics; j++)
                    {
                        dataSqlStmt += SqlServerProbe.DataValueToString(metricGroup.metrics[j].type, data.values[i, metricGroup.NumberOfMultiRowKeys + metricGroup.NumberOfMultiRowKeyAttributes + j]) + ",";
                    }

                    dataSqlStmt  = dataSqlStmt.Remove(dataSqlStmt.Length - 1);    // remove last comma
                    dataSqlStmt += "),";
                }

                dataSqlStmt = dataSqlStmt.Remove(dataSqlStmt.Length - 1);     // remove last comma
                _logger.Trace(dataSqlStmt);
                break;

            default:
                throw new Exception("Unsupported data change speed");
            }

            /*
             * Executes SQL statements
             * If tables don't exist, will try to create them and rerun SQL statements
             */
            try
            {
                // save data
                using (SqlCommand cmd = this.reposConn.CreateCommand())
                {
                    if (tran != null) // use same transaction as for the dictionary
                    {
                        cmd.Transaction = tran;
                    }

                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = dataSqlStmt;
                    int rowCount = cmd.ExecuteNonQuery();
                    _logger.Debug("Rows affected: " + rowCount.ToString());
                }

                if (tran != null)
                {
                    tran.Commit();
                }

                InMemoryCache.LoadDictionaryIntoCache(targetId, metricGroup, true);
                dictCache = Configuration.inMemoryCache[dictTableName];

                // Update in-memory data cache
                object[] newValues;

                dataCache = Configuration.inMemoryCache[dataTableName];

                for (int i = 0; i < data.NumberOfRows; i++)
                {
                    key = new object[metricGroup.NumberOfMultiRowKeys];

                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeys; j++)
                    {
                        key[j] = data.values[i, j];
                    }

                    id = dictCache.GetIdByKey(key);

                    newValues = new object[metricGroup.NumberOfMetrics];

                    for (int j = 0; j < metricGroup.NumberOfMetrics; j++)
                    {
                        newValues[j] = data.values[i, metricGroup.NumberOfMultiRowKeys + metricGroup.NumberOfMultiRowKeyAttributes + j];
                    }

                    dataCache.AddOrUpdateRowValues(id, new object[0], newValues);
                }

                canExit = true;
            }
            catch (SqlException e)
            {
                _logger.Error("SqlException: " + e.Message + " ErrorCode: " + e.Number.ToString());
                if (tran != null)
                {
                    tran.Rollback();
                    InMemoryCache.LoadDictionaryIntoCache(targetId, metricGroup, true);
                }
            }
        }
コード例 #5
0
ファイル: Writer.cs プロジェクト: radtek/sqlwristband
        } // end of GenerateSqlStaticDict method

        // Generates UPDATE statement for closing records and INSERT statement for openning records
        private static string GenerateSqlSlowDict(int targetId, MetricGroup metricGroup, ProbeResultingData data, List <Tuple <int, int> > rowsChanged, List <int> rowsNotInDict)
        {
            CacheTable dict    = Configuration.inMemoryCache[InMemoryCache.GetCacheKey(targetId, metricGroup, CacheType.Dictionary)];
            string     sqlStmt = string.Empty;

            // old rows where endDate need to be updated
            if (rowsChanged.Count > 0)
            {
                sqlStmt  = "UPDATE " + SqlServerProbe.DictTableName(targetId, metricGroup) + Environment.NewLine;
                sqlStmt += "SET endDate = '" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "'" + Environment.NewLine;
                sqlStmt += "WHERE";

                foreach (Tuple <int, int> ids in rowsChanged)
                {
                    sqlStmt += Environment.NewLine + "(id = " + ids.Item2.ToString() + " AND ";
                    sqlStmt += "startDate = '" + SqlServerProbe.DateTimeToString((DateTime)dict[ids.Item2][metricGroup.NumberOfMultiRowKeys + metricGroup.NumberOfMultiRowKeyAttributes]) + "')";
                    sqlStmt += " OR ";
                }

                sqlStmt  = sqlStmt.Remove(sqlStmt.Length - 4); // remove last ' OR '
                sqlStmt += ";" + Environment.NewLine;

                // new records for changed rows
                sqlStmt += "INSERT INTO " + SqlServerProbe.DictTableName(targetId, metricGroup) + " (id,";
                for (int i = 0; i < metricGroup.NumberOfMultiRowKeys; i++)
                {
                    sqlStmt += Environment.NewLine + metricGroup.multiRowKeys[i].name.Replace(' ', '_') + ",";
                }

                for (int i = 0; i < metricGroup.NumberOfMultiRowKeyAttributes; i++)
                {
                    sqlStmt += Environment.NewLine + metricGroup.multiRowKeyAttributes[i].name.Replace(' ', '_') + ",";
                }

                sqlStmt += "startDate,endDate)" + Environment.NewLine;
                sqlStmt += "VALUES " + Environment.NewLine;

                foreach (Tuple <int, int> ids in rowsChanged)
                {
                    sqlStmt += "(" + ids.Item2.ToString() + ",";
                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeys; j++)
                    {
                        sqlStmt += SqlServerProbe.DataValueToString(metricGroup.multiRowKeys[j].type, data.values[ids.Item1, j]) + ",";
                    }

                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeyAttributes; j++)
                    {
                        sqlStmt += SqlServerProbe.DataValueToString(metricGroup.multiRowKeyAttributes[j].type, data.values[ids.Item1, metricGroup.NumberOfMultiRowKeys + j]) + ",";
                    }

                    // add startDate and endDate
                    sqlStmt += "'" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "',NULL),";
                }

                sqlStmt = sqlStmt.Remove(sqlStmt.Length - 1); // remove last comma
            }

            // new rows
            if (rowsNotInDict.Count > 0)
            {
                sqlStmt += "INSERT INTO " + SqlServerProbe.DictTableName(targetId, metricGroup) + " (id,";
                for (int i = 0; i < metricGroup.NumberOfMultiRowKeys; i++)
                {
                    sqlStmt += Environment.NewLine + metricGroup.multiRowKeys[i].name.Replace(' ', '_') + ",";
                }

                for (int i = 0; i < metricGroup.NumberOfMultiRowKeyAttributes; i++)
                {
                    sqlStmt += Environment.NewLine + metricGroup.multiRowKeyAttributes[i].name.Replace(' ', '_') + ",";
                }

                sqlStmt += "startDate,endDate)" + Environment.NewLine;
                sqlStmt += "VALUES " + Environment.NewLine;

                foreach (int i in rowsNotInDict)
                {
                    sqlStmt += "(NEXT VALUE FOR " + SqlServerProbe.SchemaName(targetId) + ".seq_" + metricGroup.dictTableName + ",";
                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeys; j++)
                    {
                        sqlStmt += SqlServerProbe.DataValueToString(metricGroup.multiRowKeys[j].type, data.values[i, j]) + ",";
                    }

                    for (int j = 0; j < metricGroup.NumberOfMultiRowKeyAttributes; j++)
                    {
                        sqlStmt += SqlServerProbe.DataValueToString(metricGroup.multiRowKeyAttributes[j].type, data.values[i, metricGroup.NumberOfMultiRowKeys + j]) + ",";
                    }

                    // add startDate and endDate
                    sqlStmt += "'" + SqlServerProbe.DateTimeToString(data.probeDateTime) + "',NULL),";
                }

                sqlStmt = sqlStmt.Remove(sqlStmt.Length - 1); // remove last comma
            }

            return(sqlStmt);
        } // end of GenerateSqlSlowDict method