private static void WaitUntilTableReady(string tableName)
        {
            string status = null;

            // Let us wait until table is created. Call DescribeTable.
            do
            {
                System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
                try
                {
                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });

                    Console.WriteLine("Table name: {0}, status: {1}",
                                      res.Table.TableName,
                                      res.Table.TableStatus);
                    status = res.Table.TableStatus;
                }
                catch (ResourceNotFoundException)
                {
                    // DescribeTable is eventually consistent. So you might
                    // get resource not found. So we handle the potential exception.
                }
            } while (status != "ACTIVE");
        }
Ejemplo n.º 2
0
        public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
        {
            AssertTableExists();

            var max = pageSize * (pageIndex + 1);

            Dictionary <string, AttributeValue> lastEvaluatedKey = null;
            var errors = new List <ErrorLogEntry>(max);

            // have to start at the beginning and go through up to the current page, this means it will perform worse as we go through more pages
            // usually, we are just looking at the first few pages so ¯\_(ツ)_/¯

            // low level scanning http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetScanning.html#LowLevelDotNetScanningOptions
            do
            {
                var request = new QueryRequest(TableName)
                {
                    KeyConditionExpression    = "Application = :v_appl",
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>
                    {
                        { ":v_appl", new AttributeValue(ApplicationName) }
                    },
                    IndexName        = "Application-TimeUtc-index",
                    ScanIndexForward = false,
                    Limit            = max,
                    Select           = Select.ALL_PROJECTED_ATTRIBUTES
                };
                if (lastEvaluatedKey != null)
                {
                    request.ExclusiveStartKey = lastEvaluatedKey;
                }

                var response = _client.Query(request);
                errors.AddRange(from item in response.Items
                                let errorXml = item["AllXml"].S
                                               let errorId                         = item["ErrorId"].S
                                                                         let error = ErrorXml.DecodeString(errorXml)
                                                                                     select new ErrorLogEntry(this, errorId, error));

                lastEvaluatedKey = response.LastEvaluatedKey;
            } while (lastEvaluatedKey != null && lastEvaluatedKey.Count > 0 && errors.Count < max);

            var numberToSkip = pageIndex * pageSize;

            errors = errors.Skip(numberToSkip).Take(pageSize).ToList();
            errors.ForEach(err => errorEntryList.Add(err));

            // get total count of items in the table.
            // This value is stale (updates every six hours) but will do the job in most cases

            // the other alternative would be to do another scan of the entire index with a Select.COUNT

            var total = _client.DescribeTable(new DescribeTableRequest(TableName)).Table.ItemCount;

            return(Convert.ToInt32(Math.Max(errorEntryList.Count, total)));
        }
Ejemplo n.º 3
0
        public static void WaitUntilTableReady(this AmazonDynamoDBClient client, string tableName, string currentStatus = null)
        {
            int attempts = 0;

            if (!string.IsNullOrEmpty(currentStatus) && !currentStatus.Equals("ACTIVE", StringComparison.OrdinalIgnoreCase))
            {
                string status = null;
                // Let us wait until table is created. Call DescribeTable.
                do
                {
                    if (attempts > 10)
                    {
                        throw new TimeoutException("Waited for 30 seconds for table " + tableName + " to be created.");
                    }

                    System.Threading.Thread.Sleep(3000); // Wait 3 seconds.
                    try
                    {
                        attempts++;
                        var res = client.DescribeTable(new DescribeTableRequest {
                            TableName = tableName
                        });

                        Console.WriteLine("Table name: {0}, status: {1}",
                                          res.Table.TableName,
                                          res.Table.TableStatus);
                        status = res.Table.TableStatus;
                    }
                    catch (ResourceNotFoundException)
                    {
                        // Table is eventually consistent. So you might get resource not found. So we handle the potential exception.
                    }
                } while (status != "ACTIVE");
            }
        }
Ejemplo n.º 4
0
        public static void WaitUntilTableDeleted(this AmazonDynamoDBClient client, string tableName)
        {
            int    attempts = 0;
            string status   = null;

            // Let us wait until table is deleted. Call DescribeTable.
            do
            {
                if (attempts > 10)
                {
                    throw new TimeoutException("Waited for 30 seconds for table " + tableName + " to be deleted.");
                }

                attempts++;
                System.Threading.Thread.Sleep(3000); // Wait 5 seconds.
                try
                {
                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });

                    status = res.Table.TableStatus;
                }
                catch (ResourceNotFoundException)
                {
                    // Table not found. It is deleted
                    return;
                }
            } while (status == "DELETING");
        }
        private void WaitUntilTableReady(string tableName, string currentStatus)
        {
            if (!currentStatus.Equals("ACTIVE", StringComparison.OrdinalIgnoreCase))
            {
                string status = null;
                // Let us wait until table is created. Call DescribeTable.
                do
                {
                    System.Threading.Thread.Sleep(3000); // Wait 3 seconds.
                    try
                    {
                        var res = _client.DescribeTable(new DescribeTableRequest {
                            TableName = tableName
                        });

                        Console.WriteLine("Table name: {0}, status: {1}",
                                          res.Table.TableName,
                                          res.Table.TableStatus);
                        status = res.Table.TableStatus;
                    }
                    catch (ResourceNotFoundException)
                    {
                        // Table is eventually consistent. So you might  get resource not found. So we handle the potential exception.
                    }
                } while (status != "ACTIVE");
            }
        }
        private void WaitTillTableDeleted(AmazonDynamoDBClient client, string tableName)
        {
            Console.Write("Deleting the table: ");
            string status;

            try {
                do
                {
                    System.Threading.Thread.Sleep(50);

                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });
                    if (res.Table.TableStatus == "DELETING")
                    {
                        Console.Write(".");
                    }
                    else
                    {
                        Console.Write("[{0}]", res.Table.TableStatus);
                    }
                    status = res.Table.TableStatus;
                }while (status == "DELETING");
            }
            catch (ResourceNotFoundException) {
                Console.WriteLine(" Done.");
            }
        }
Ejemplo n.º 7
0
        private static void WaitTillTableDeleted(AmazonDynamoDBClient client, string tableName,
                                                 DeleteTableResponse response)
        {
            var tableDescription = response.TableDescription;

            string status = tableDescription.TableStatus;

            Console.WriteLine(tableName + " - " + status);

            // Let us wait until table is created. Call DescribeTable
            try
            {
                while (status == "DELETING")
                {
                    System.Threading.Thread.Sleep(5000); // wait 5 seconds

                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });
                    Console.WriteLine("Table name: {0}, status: {1}", res.Table.TableName,
                                      res.Table.TableStatus);
                    status = res.Table.TableStatus;
                }
            }
            catch (ResourceNotFoundException)
            {
                // Table deleted.
            }
        }
        private void WaitTillTableCreated(AmazonDynamoDBClient client, string tableName)
        {
            string status;

            Console.Write("Creating the table: ");
            do
            {
                System.Threading.Thread.Sleep(50);
                try {
                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });
                    if (res.Table.TableStatus == "ACTIVE")
                    {
                        break;
                    }
                    if (res.Table.TableStatus == "CREATING")
                    {
                        Console.Write(".");
                    }
                    else
                    {
                        Console.Write("[{0}]", res.Table.TableStatus);
                    }

                    status = res.Table.TableStatus;
                }
                catch (ResourceNotFoundException) {
                    Console.Write("?");  // Okay to appear, since the consistency is eventual.
                    status = "EXCEPTION";
                }
            } while (status != "ACTIVE");
            Console.WriteLine(" Done.");
        }
Ejemplo n.º 9
0
        private static void WaitTillTableCreated(AmazonDynamoDBClient client, string tableName,
                                                 CreateTableResponse response)
        {
            var tableDescription = response.TableDescription;

            string status = tableDescription.TableStatus;

            Console.WriteLine(tableName + " - " + status);

            // Let us wait until table is created. Call DescribeTable.
            while (status != "ACTIVE")
            {
                System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
                try
                {
                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });
                    Console.WriteLine("Table name: {0}, status: {1}", res.Table.TableName,
                                      res.Table.TableStatus);
                    status = res.Table.TableStatus;
                }
                // Try-catch to handle potential eventual-consistency issue.
                catch (ResourceNotFoundException)
                { }
            }
        }
Ejemplo n.º 10
0
        private static void WaitUntilTableReady(string tableName)
        {
            var config = new AmazonDynamoDBConfig();

            config.ServiceURL = "http://dynamodb.us-east-1.amazonaws.com";
            client            = new AmazonDynamoDBClient(config);
            string status = null;

            // Let us wait until table is created. Call DescribeTable.
            do
            {
                System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
                try
                {
                    var res = client.DescribeTable(new DescribeTableRequest
                    {
                        TableName = tableName
                    });

                    Console.WriteLine("Table name: {0}, status: {1}",
                                      res.DescribeTableResult.Table.TableName,
                                      res.DescribeTableResult.Table.TableStatus);
                    status = res.DescribeTableResult.Table.TableStatus;
                }
                catch (Amazon.DynamoDBv2.Model.ResourceNotFoundException resourceNotFound)
                {
                    // DescribeTable is eventually consistent. So you might
                    // get resource not found. So we handle the potential exception.
                }
            } while (status != "ACTIVE");
        }
Ejemplo n.º 11
0
        public void TestCache()
        {
            Func <string, TableDescription> creator = tn => client.DescribeTable(tn).Table;

            var tableName  = GetTableName();
            var tableCache = SdkCache.GetCache <string, TableDescription>(client, DynamoDBTests.TableCacheIdentifier, StringComparer.Ordinal);

            Assert.AreEqual(0, tableCache.ItemCount);

            using (var counter = new ServiceResponseCounter(client))
            {
                var table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(1, counter.ResponseCount);
                Assert.AreEqual(1, tableCache.ItemCount);

                // verify the item is still there
                table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(1, counter.ResponseCount);

                // verify item was reloaded
                tableCache.Clear(tableName);
                table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(2, counter.ResponseCount);
                Assert.AreEqual(1, tableCache.ItemCount);

                // test item expiration
                tableCache.MaximumItemLifespan = TimeSpan.FromSeconds(1);
                Thread.Sleep(tableCache.MaximumItemLifespan);
                table = tableCache.GetValue(tableName, creator);
                Assert.AreEqual(3, counter.ResponseCount);
                Assert.AreEqual(1, tableCache.ItemCount);
            }
        }
Ejemplo n.º 12
0
        // GET api/<controller>/5
        public DescribeTableResponse Get(string id)
        {
            var response = _client.ListTables();

            return(response.TableNames.Contains(id)
                                ? _client.DescribeTable(id)
                                : null);
        }
Ejemplo n.º 13
0
 private static bool GetTableInformation()
 {
     Console.WriteLine("\n*** Retrieving table information ***");
     var request = new DescribeTableRequest
     {
         TableName = "Employee"
     };
     try
     {
         var response = ddbClient.DescribeTable(request);
         TableDescription description = response.Table;
         return true;
     }
     catch(Amazon.DynamoDBv2.Model.ResourceNotFoundException ex)
     {
         return false;
     }
 }
Ejemplo n.º 14
0
        private static bool PharmaDataExists()
        {
            bool pharmaDataExists = false;

            try
            {
                dynamoDBClient.DescribeTable(new DescribeTableRequest
                {
                    TableName = PharmaTableName
                });

                pharmaDataExists = true;
            }
            catch (ResourceNotFoundException e)
            {
                Debug.WriteLine(PharmaTableName + " DynamoDB table does not exist. " + e.Message);
            }
            return(pharmaDataExists);
        }
Ejemplo n.º 15
0
        private static void GetTableInformation()
        {
            Console.WriteLine("\n*** Retrieving table information ***");
            var request = new DescribeTableRequest
            {
                TableName = tableName
            };

            var response = client.DescribeTable(request);

            TableDescription description = response.Table;

            Console.WriteLine("Name: {0}", description.TableName);
            Console.WriteLine("# of items: {0}", description.ItemCount);
            Console.WriteLine("Provision Throughput (reads/sec): {0}",
                              description.ProvisionedThroughput.ReadCapacityUnits);
            Console.WriteLine("Provision Throughput (writes/sec): {0}",
                              description.ProvisionedThroughput.WriteCapacityUnits);
        }
Ejemplo n.º 16
0
        public void DescribeTable()
        {
            DescribeTableRequest request = new DescribeTableRequest {
                TableName = TableName
            };
            var response = client.DescribeTable(request);

            if (response.HttpStatusCode.IsSuccess())
            {
                Console.WriteLine($"TableArn: {response.Table.TableArn}");
            }
        }
Ejemplo n.º 17
0
        /*--------------------------------------------------------------------------
         *                      checkingTableExistence
         *--------------------------------------------------------------------------*/

        public bool CheckTableIsReady(string tableName)
        {
            try
            {
                var result = DynamoClient.DescribeTable(tableName);
                return(result?.Table != null && result.Table.TableStatus == TableStatus.ACTIVE);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 18
0
        private void CreateTable(string tabNam, string hashKey)
        {
            client = new AmazonDynamoDBClient();
            var tableResponse = client.ListTables();

            if (!tableResponse.TableNames.Contains(tabNam))
            {
                MessageBox.Show("Shelf doesn't exist, creating shelf " + tabNam);
                client.CreateTable(new CreateTableRequest
                {
                    TableName             = tabNam,
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 3,
                        WriteCapacityUnits = 1
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = hashKey,
                            KeyType       = KeyType.HASH
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition {
                            AttributeName = hashKey, AttributeType = ScalarAttributeType.S
                        }
                    }
                });

                bool isTableAvailable = false;
                while (!isTableAvailable)
                {
                    Console.WriteLine("Waiting for table to be active...");
                    Thread.Sleep(5000);
                    var tableStatus = client.DescribeTable(tabNam);
                    isTableAvailable = tableStatus.Table.TableStatus == "ACTIVE";
                }
                MessageBox.Show($"Shelf {tabNam} Created Successfully!");
                SaveBookinShelf();
                enableBtns();
                //resetBtns();
            }
            else
            {
                MessageBox.Show($"{tabNam} exist already!, Adding {txtTitle.Text} book in the shelf");
                SaveBookinShelf();
                //resetBtns();
                enableBtns();
            }
        }
Ejemplo n.º 19
0
        private void AssertDynamoDbExists(string tableName)
        {
            var request = new DescribeTableRequest {
                TableName = tableName
            };

            try
            {
                var response = _client.DescribeTable(request);
            }
            catch (ResourceNotFoundException)
            {
                Assert.Fail("Table {0} doesn't exist!", _tableName);
            }
        }
Ejemplo n.º 20
0
 private static void WaitUntilTableActive(AmazonDynamoDBClient client)
 {
     Console.WriteLine("Waiting for table to be active");
     while (true)
     {
         var status = client.DescribeTable(tableName).Table.TableStatus;
         if (status == TableStatus.ACTIVE)
         {
             break;
         }
         Console.WriteLine("Table status = {0}, sleeping...", status);
         Thread.Sleep(TimeSpan.FromSeconds(1));
     }
     Console.WriteLine("Table created");
 }
Ejemplo n.º 21
0
        public void GetInformationTable()
        {
            DescribeTableRequest request = new DescribeTableRequest
            {
                TableName = "DynamoDbEmployeeBackup"
            };

            var response = client.DescribeTable(request);

            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine($"Table ARN =>{response.Table.TableArn}");
                Console.WriteLine($"Table ARN =>{response.Table.TableId}");
                Console.WriteLine($"Table ARN =>{response.Table.TableName}");
            }
            Console.ReadLine();
        }
Ejemplo n.º 22
0
        private void CreateTable()
        {
            var credentials = new BasicAWSCredentials(accessKey, secretKey);

            client = new AmazonDynamoDBClient(credentials, RegionEndpoint.USWest1);

            var tableResponse = client.ListTables();

            if (!tableResponse.TableNames.Contains(tableName))
            {
                MessageBox.Show("Table not found, creating table => " + tableName);
                client.CreateTable(new CreateTableRequest
                {
                    TableName             = tableName,
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 3,
                        WriteCapacityUnits = 1
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = hashKey,
                            KeyType       = KeyType.HASH
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition {
                            AttributeName = hashKey, AttributeType = ScalarAttributeType.S
                        }
                    }
                });

                bool isTableAvailable = false;
                while (!isTableAvailable)
                {
                    Console.WriteLine("Waiting for table to be active...");
                    Thread.Sleep(5000);
                    var tableStatus = client.DescribeTable(tableName);
                    isTableAvailable = tableStatus.Table.TableStatus == "ACTIVE";
                }
                MessageBox.Show("DynamoDB Table Created Successfully!");
            }
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Retrieves a table status. Returns empty string if table does not exist.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="tableName"></param>
 /// <returns></returns>
 private static TableStatus GetTableStatus(AmazonDynamoDBClient client, string tableName)
 {
     try
     {
         var table = client.DescribeTable(new DescribeTableRequest {
             TableName = tableName
         }).Table;
         return((table == null) ? null : table.TableStatus);
     }
     catch (AmazonDynamoDBException db)
     {
         if (db.ErrorCode == "ResourceNotFoundException")
         {
             return(string.Empty);
         }
         throw;
     }
 }
Ejemplo n.º 24
0
        private static bool DoesTableExist(AmazonDynamoDBClient client)
        {
            bool tableExists;

            try
            {
                Console.WriteLine("Testing if table exists");
                client.DescribeTable(tableName);
                Console.WriteLine("Table exists");
                tableExists = true;
            }
            catch
            {
                Console.WriteLine("Table does not exist");
                tableExists = false;
            }

            return(tableExists);
        }
Ejemplo n.º 25
0
        /**
         * Check the table's status using the DescribeTable method
         * Wait for the table to become active
         *
         * @return      Table Status
         */
        private static string GetTableStatus()
        {
            string status = null;

            System.Threading.Thread.Sleep(5000);
            try
            {
                var res = dynamoDBClient.DescribeTable(new DescribeTableRequest
                {
                    TableName = InfectionsTableName
                });
                status = res.Table.TableStatus;
            }
            catch (AmazonDynamoDBException ex)
            {
                Debug.WriteLine("An exception has occured: " + ex.Message);
            }
            return(status);
        }
Ejemplo n.º 26
0
        public static string GetTableStatus(AmazonDynamoDBClient dynamoDBClient, string InfectionsTableName)
        {
            Debug.WriteLine(String.Format("RUNNING SOLUTION CODE: {0}! Follow the steps in the lab guide to replace this method with your own implementation.\n", "GetTableStatus"));
            string status = null;

            System.Threading.Thread.Sleep(5000);
            try
            {
                var res = dynamoDBClient.DescribeTable(new DescribeTableRequest
                {
                    TableName = InfectionsTableName
                });
                status = res.Table.TableStatus;
            }
            catch (AmazonDynamoDBException ex)
            {
                Debug.WriteLine("An exception has occured: " + ex.Message);
            }
            return(status);
        }
Ejemplo n.º 27
0
        public DDBOperation()
        {
            credentials = new Amazon.Runtime.BasicAWSCredentials(
                ConfigurationManager.AppSettings["accessId"],
                ConfigurationManager.AppSettings["secretKey"]);
            client  = new AmazonDynamoDBClient(credentials, Amazon.RegionEndpoint.USEast1);
            context = new DynamoDBContext(client);

            // Check the existence of the User database and creating one if it does not exist
            try
            {
                var res = client.DescribeTable(new DescribeTableRequest {
                    TableName = "User"
                });
            }
            catch (Exception)
            {
                CreateTable();
            }
        }
Ejemplo n.º 28
0
        private static void WaitTilTableCreated(string tableName, CreateTableResponse response)
        {
            var tableDescription = response.TableDescription;

            string status = tableDescription.TableStatus;

            Util.LogInfo(tableName + " - " + status);

            // Let us wait until table is created. Call DescribeTable.
            while (status != "ACTIVE")
            {
                Thread.Sleep(5000);                   // Wait 5 seconds.
                try {
                    var res = client.DescribeTable(new DescribeTableRequest {
                        TableName = tableName
                    });
                    Util.LogInfo("Table name: " + res.Table.TableName + ", status: " + res.Table.TableStatus);
                    status = res.Table.TableStatus;
                }
                // Try-catch to handle potential eventual-consistency issue.
                catch (ResourceNotFoundException) { }
            }
        }
Ejemplo n.º 29
0
        public virtual TableDescription GetTableDescription(AmazonDynamoDBClient ddbClient, string tableName)
        {
            try
            {
                DescribeTableResponse describeTableResponse = ddbClient.DescribeTable(
                    new DescribeTableRequest
                {
                    TableName = tableName
                });

                return(describeTableResponse.Table);
            }
            catch (AmazonServiceException ase)
            {
                // If the table isn't found, there's no problem.
                // If the error is something else, rethrow the exception to bubble it up to the caller.
                if (!ase.ErrorCode.Equals("ResourceNotFoundException"))
                {
                    throw;
                }
                return(null);
            }
        }
        private static void WaitUntilTableReady(AmazonDynamoDBClient client, TableStatus targetStatus)
        {
            var         startTime = DateTime.Now;
            TableStatus status;

            while ((DateTime.Now - startTime) < tableActiveMaxTime)
            {
                try
                {
                    status = client.DescribeTable(tableName).Table.TableStatus;
                }
                catch (ResourceNotFoundException)
                {
                    status = null;
                }

                if (status == targetStatus)
                {
                    return;
                }
                Thread.Sleep(waitPeriod);
            }
        }