Skip to content

Commit

Permalink
Remove all sync APIs
Browse files Browse the repository at this point in the history
Sync API simply calls .Result or .Wait, which will cause deadlocks in
environments like WPF or MVC. This is a trap that confuses many people.
  • Loading branch information
duoxu committed Mar 25, 2016
1 parent bc0da87 commit 1ac87c0
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 446 deletions.
62 changes: 31 additions & 31 deletions Microsoft.HBase.Client.Tests/CellOperationsTests.cs
Expand Up @@ -51,13 +51,13 @@ protected override void Context()
{
var client = GetClient();
// ensure tables from previous tests are cleaned up
TableList tables = client.ListTables();
TableList tables = client.ListTablesAsync().Result;
foreach (string name in tables.name)
{
string pinnedName = name;
if (name.StartsWith(TableNamePrefix, StringComparison.Ordinal))
{
client.DeleteTable(pinnedName);
client.DeleteTableAsync(pinnedName).Wait();
}
}

Expand Down Expand Up @@ -90,14 +90,14 @@ public void When_I_DeleteCells_With_TimeStamp_I_can_add_with_higher_timestamp()
{
var client = GetClient();

client.StoreCells(_tableName, CreateCellSet(GetCellSet("1", "c1", "1A", 10)));
client.StoreCells(_tableName, CreateCellSet(GetCellSet("1", "c2", "1A", 10)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("1", "c1", "1A", 10))).Wait();
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("1", "c2", "1A", 10))).Wait();

client.DeleteCells(_tableName, "1", ColumnFamilyName1, 10);
client.DeleteCellsAsync(_tableName, "1", ColumnFamilyName1, 10).Wait();

try
{
client.GetCells(_tableName, "1");
client.GetCellsAsync(_tableName, "1").Wait();
Assert.Fail("Expected to throw an exception as the row is deleted");
}
catch(Exception ex)
Expand All @@ -108,9 +108,9 @@ public void When_I_DeleteCells_With_TimeStamp_I_can_add_with_higher_timestamp()
}
}

client.StoreCells(_tableName, CreateCellSet(GetCellSet("1", "c1", "1A", 11)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("1", "c1", "1A", 11))).Wait();

var retrievedCells = client.GetCells(_tableName, "1");
var retrievedCells = client.GetCellsAsync(_tableName, "1").Result;

retrievedCells.rows.Count.ShouldEqual(1);
retrievedCells.rows[0].values[0].timestamp.ShouldEqual(11);
Expand All @@ -122,14 +122,14 @@ public void When_I_DeleteCells_With_TimeStamp_I_cannot_add_with_lower_timestamp(
{
var client = GetClient();

client.StoreCells(_tableName, CreateCellSet(GetCellSet("2", "c1", "1A", 10)));
client.StoreCells(_tableName, CreateCellSet(GetCellSet("2", "c2", "1A", 10)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("2", "c1", "1A", 10))).Wait();
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("2", "c2", "1A", 10))).Wait();

client.DeleteCells(_tableName, "2", ColumnFamilyName1, 10);
client.DeleteCellsAsync(_tableName, "2", ColumnFamilyName1, 10).Wait();

try
{
client.GetCells(_tableName, "2");
client.GetCellsAsync(_tableName, "2").Wait();
Assert.Fail("Expected to throw an exception as the row is deleted");
}
catch (Exception ex)
Expand All @@ -140,11 +140,11 @@ public void When_I_DeleteCells_With_TimeStamp_I_cannot_add_with_lower_timestamp(
}
}

client.StoreCells(_tableName, CreateCellSet(GetCellSet("2", "c1", "1A", 9)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("2", "c1", "1A", 9))).Wait();

try
{
client.GetCells(_tableName, "2");
client.GetCellsAsync(_tableName, "2").Wait();
Assert.Fail("Expected to throw an exception as the row cannot be added with lower timestamp");
}
catch (Exception ex)
Expand All @@ -163,14 +163,14 @@ public async Task When_I_CheckAndDeleteCells_With_TimeStamp_I_cannot_add_with_lo
{
var client = GetClient();

client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c1", "1A", 10)));
client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c2", "1A", 10)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1A", 10))).Wait();
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c2", "1A", 10))).Wait();

bool deleted = await client.CheckAndDeleteAsync(_tableName, GetCell("3","c1","1A",10));

deleted.ShouldEqual(true);

var retrievedCells = client.GetCells(_tableName, "3");
var retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
// Deletes in the Cell c1 so c2 should be present.
retrievedCells.rows[0].values.Count.ShouldEqual(1);

Expand All @@ -180,7 +180,7 @@ public async Task When_I_CheckAndDeleteCells_With_TimeStamp_I_cannot_add_with_lo
try
{
// All cells are deleted so this should fail
retrievedCells = client.GetCells(_tableName, "3");
retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
throw new AssertFailedException("expecting Get '3' to fail as all cells are removed");
}
catch (Exception ex)
Expand All @@ -191,11 +191,11 @@ public async Task When_I_CheckAndDeleteCells_With_TimeStamp_I_cannot_add_with_lo
}
}

client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11))).Wait();

try
{
retrievedCells = client.GetCells(_tableName, "3");
retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
throw new AssertFailedException("Expected to throw an exception as the row cannot be added with lower timestamp than servers timestamp");
}
catch (Exception ex)
Expand All @@ -214,8 +214,8 @@ public async Task When_I_CheckAndDeleteCells_With_TimeStamp_And_Cells_To_Delete_
{
var client = GetClient();

client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c1", "1A", 10)));
client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c2", "1A", 10)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1A", 10))).Wait();
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c2", "1A", 10))).Wait();

// Deletes all the ColumnFamily with timestamp less than 10
CellSet.Row rowToDelete = new CellSet.Row() { key = Encoding.UTF8.GetBytes("3") };
Expand All @@ -230,7 +230,7 @@ public async Task When_I_CheckAndDeleteCells_With_TimeStamp_And_Cells_To_Delete_
try
{
// All cells are deleted so this should fail
retrievedCells = client.GetCells(_tableName, "3");
retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
throw new AssertFailedException("expecting Get '3' to fail as all cells are removed");
}
catch (Exception ex)
Expand All @@ -245,11 +245,11 @@ public async Task When_I_CheckAndDeleteCells_With_TimeStamp_And_Cells_To_Delete_
}
}

client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11))).Wait();

try
{
retrievedCells = client.GetCells(_tableName, "3");
retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
retrievedCells.rows[0].values.Count.ShouldEqual(1);
Encoding.UTF8.GetString(retrievedCells.rows[0].values[0].column).ShouldBeEqualOrdinalIgnoreCase("c1");
}
Expand All @@ -268,8 +268,8 @@ public async Task When_I_CheckAndDeleteCells_With_ColumnFamily_Deletes_All_cells
{
var client = GetClient();

client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c1", "1A", 10)));
client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c2", "1A", 10)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1A", 10))).Wait();
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c2", "1A", 10))).Wait();

// Deletes all the ColumnFamily with timestamp less than 10
CellSet.Row rowToDelete = new CellSet.Row() { key = Encoding.UTF8.GetBytes("3") };
Expand All @@ -282,7 +282,7 @@ public async Task When_I_CheckAndDeleteCells_With_ColumnFamily_Deletes_All_cells
try
{
// All cells are deleted so this should fail
retrievedCells = client.GetCells(_tableName, "3");
retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
throw new AssertFailedException("expecting Get '3' to fail as all cells are removed");
}
catch (Exception ex)
Expand All @@ -293,11 +293,11 @@ public async Task When_I_CheckAndDeleteCells_With_ColumnFamily_Deletes_All_cells
}
}

client.StoreCells(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11)));
client.StoreCellsAsync(_tableName, CreateCellSet(GetCellSet("3", "c1", "1B", 11))).Wait();

try
{
retrievedCells = client.GetCells(_tableName, "3");
retrievedCells = client.GetCellsAsync(_tableName, "3").Result;
retrievedCells.rows[0].values.Count.ShouldEqual(1);
}
catch (Exception ex)
Expand Down Expand Up @@ -367,7 +367,7 @@ private void AddTable()
_tableSchema.columns.Add(new ColumnSchema { name = ColumnFamilyName1 });
_tableSchema.columns.Add(new ColumnSchema { name = ColumnFamilyName2 });

client.CreateTable(_tableSchema);
client.CreateTableAsync(_tableSchema).Wait();
}
}
}
24 changes: 12 additions & 12 deletions Microsoft.HBase.Client.Tests/Clients/GatewayClientTest.cs
Expand Up @@ -53,11 +53,11 @@ public override void TestFullScan()
ScannerInformation scannerInfo = null;
try
{
scannerInfo = client.CreateScanner(testTableName, scanSettings, scanOptions);
scannerInfo = client.CreateScannerAsync(testTableName, scanSettings, scanOptions).Result;

CellSet next;
var expectedSet = new HashSet<int>(Enumerable.Range(0, 100));
while ((next = client.ScannerGetNext(scannerInfo, scanOptions)) != null)
while ((next = client.ScannerGetNextAsync(scannerInfo, scanOptions).Result) != null)
{
Assert.AreEqual(10, next.rows.Count);
foreach (CellSet.Row row in next.rows)
Expand All @@ -72,7 +72,7 @@ public override void TestFullScan()
{
if (scannerInfo != null)
{
client.DeleteScanner(testTableName, scannerInfo, scanOptions);
client.DeleteScannerAsync(testTableName, scannerInfo, scanOptions).Wait();
}
}
}
Expand All @@ -89,7 +89,7 @@ public override void TestScannerCreation()
ScannerInformation scannerInfo = null;
try
{
scannerInfo = client.CreateScanner(testTableName, scanSettings, scanOptions);
scannerInfo = client.CreateScannerAsync(testTableName, scanSettings, scanOptions).Result;
Assert.AreEqual(testTableName, scannerInfo.TableName);
Assert.IsNotNull(scannerInfo.ScannerId);
Assert.IsFalse(scannerInfo.ScannerId.StartsWith("/"), "scanner id starts with a slash");
Expand All @@ -99,7 +99,7 @@ public override void TestScannerCreation()
{
if (scannerInfo != null)
{
client.DeleteScanner(testTableName, scannerInfo, scanOptions);
client.DeleteScannerAsync(testTableName, scannerInfo, scanOptions).Wait();
}
}
}
Expand All @@ -119,22 +119,22 @@ public override void TestScannerDeletion()

try
{
scannerInfo = client.CreateScanner(testTableName, scanSettings, scanOptions);
scannerInfo = client.CreateScannerAsync(testTableName, scanSettings, scanOptions).Result;
Assert.AreEqual(testTableName, scannerInfo.TableName);
Assert.IsNotNull(scannerInfo.ScannerId);
Assert.IsFalse(scannerInfo.ScannerId.StartsWith("/"), "scanner id starts with a slash");
Assert.IsNotNull(scannerInfo.ResponseHeaderCollection);
// delete the scanner
client.DeleteScanner(testTableName, scannerInfo, scanOptions);
client.DeleteScannerAsync(testTableName, scannerInfo, scanOptions).Wait();
// try to fetch data use the deleted scanner
scanOptions.RetryPolicy = RetryPolicy.NoRetry;
client.ScannerGetNext(scannerInfo, scanOptions);
client.ScannerGetNextAsync(scannerInfo, scanOptions).Wait();
}
finally
{
if (scannerInfo != null)
{
client.DeleteScanner(testTableName, scannerInfo, scanOptions);
client.DeleteScannerAsync(testTableName, scannerInfo, scanOptions).Wait();
}
}
}
Expand All @@ -155,11 +155,11 @@ public override void TestSubsetScan()
ScannerInformation scannerInfo = null;
try
{
scannerInfo = client.CreateScanner(testTableName, scanSettings, scanOptions);
scannerInfo = client.CreateScannerAsync(testTableName, scanSettings, scanOptions).Result;

CellSet next;
var expectedSet = new HashSet<int>(Enumerable.Range(startRow, endRow - startRow));
while ((next = client.ScannerGetNext(scannerInfo, scanOptions)) != null)
while ((next = client.ScannerGetNextAsync(scannerInfo, scanOptions).Result) != null)
{
foreach (CellSet.Row row in next.rows)
{
Expand All @@ -173,7 +173,7 @@ public override void TestSubsetScan()
{
if (scannerInfo != null)
{
client.DeleteScanner(testTableName, scannerInfo, scanOptions);
client.DeleteScannerAsync(testTableName, scannerInfo, scanOptions).Wait();
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions Microsoft.HBase.Client.Tests/Clients/HBaseClientTestBase.cs
Expand Up @@ -41,12 +41,12 @@ protected override void Context()
var client = CreateClient();

// ensure tables from previous tests are cleaned up
TableList tables = client.ListTables();
TableList tables = client.ListTablesAsync().Result;
foreach (string name in tables.name)
{
if (name.StartsWith(TestTablePrefix, StringComparison.Ordinal))
{
client.DeleteTable(name);
client.DeleteTableAsync(name).Wait();
}
}

Expand All @@ -56,7 +56,7 @@ protected override void Context()
_testTableSchema.name = testTableName;
_testTableSchema.columns.Add(new ColumnSchema { name = "d" });

client.CreateTable(_testTableSchema);
client.CreateTableAsync(_testTableSchema).Wait();
}

public abstract IHBaseClient CreateClient();
Expand Down Expand Up @@ -85,22 +85,22 @@ public void TestCellsDeletion()
var value = new Cell { column = Encoding.UTF8.GetBytes("d:starwars"), data = Encoding.UTF8.GetBytes(testValue) };
row.values.Add(value);

client.StoreCells(testTableName, set);
CellSet cell = client.GetCells(testTableName, testKey);
client.StoreCellsAsync(testTableName, set).Wait();
CellSet cell = client.GetCellsAsync(testTableName, testKey).Result;
// make sure the cell is in the table
Assert.AreEqual(Encoding.UTF8.GetString(cell.rows[0].key), testKey);
// delete cell
client.DeleteCells(testTableName, testKey);
client.DeleteCellsAsync(testTableName, testKey).Wait();
// get cell again, 404 exception expected
client.GetCells(testTableName, testKey);
client.GetCellsAsync(testTableName, testKey).Wait();
}

[TestMethod]
[TestCategory(TestRunMode.CheckIn)]
public void TestGetStorageClusterStatus()
{
var client = CreateClient();
StorageClusterStatus status = client.GetStorageClusterStatus();
StorageClusterStatus status = client.GetStorageClusterStatusAsync().Result;
// TODO not really a good test
Assert.IsTrue(status.requests >= 0, "number of requests is negative");
Assert.IsTrue(status.liveNodes.Count >= 1, "number of live nodes is zero or negative");
Expand All @@ -112,7 +112,7 @@ public void TestGetStorageClusterStatus()
public void TestGetVersion()
{
var client = CreateClient();
org.apache.hadoop.hbase.rest.protobuf.generated.Version version = client.GetVersion();
org.apache.hadoop.hbase.rest.protobuf.generated.Version version = client.GetVersionAsync().Result;

Trace.WriteLine(version);

Expand All @@ -128,7 +128,7 @@ public void TestListTables()
{
var client = CreateClient();

TableList tables = client.ListTables();
TableList tables = client.ListTablesAsync().Result;
List<string> testtables = tables.name.Where(item => item.StartsWith("marlintest", StringComparison.Ordinal)).ToList();
Assert.AreEqual(1, testtables.Count);
Assert.AreEqual(testTableName, testtables[0]);
Expand All @@ -152,9 +152,9 @@ public void TestStoreSingleCell()
var value = new Cell { column = Encoding.UTF8.GetBytes("d:starwars"), data = Encoding.UTF8.GetBytes(testValue) };
row.values.Add(value);

client.StoreCells(testTableName, set);
client.StoreCellsAsync(testTableName, set).Wait();

CellSet cells = client.GetCells(testTableName, testKey);
CellSet cells = client.GetCellsAsync(testTableName, testKey).Result;
Assert.AreEqual(1, cells.rows.Count);
Assert.AreEqual(1, cells.rows[0].values.Count);
Assert.AreEqual(testValue, Encoding.UTF8.GetString(cells.rows[0].values[0].data));
Expand All @@ -169,7 +169,7 @@ public void TestStoreSingleCell()
public void TestTableSchema()
{
var client = CreateClient();
TableSchema schema = client.GetTableSchema(testTableName);
TableSchema schema = client.GetTableSchemaAsync(testTableName).Result;
Assert.AreEqual(testTableName, schema.name);
Assert.AreEqual(_testTableSchema.columns.Count, schema.columns.Count);
Assert.AreEqual(_testTableSchema.columns[0].name, schema.columns[0].name);
Expand All @@ -188,7 +188,7 @@ public void StoreTestData(IHBaseClient hBaseClient)
set.rows.Add(row);
}

hBaseClient.StoreCells(testTableName, set);
hBaseClient.StoreCellsAsync(testTableName, set).Wait();
}
}
}

0 comments on commit 1ac87c0

Please sign in to comment.