2121import com .alipay .oceanbase .hbase .OHTableClient ;
2222import com .alipay .oceanbase .hbase .util .ObHTableTestUtil ;
2323import com .alipay .oceanbase .hbase .util .TableTemplateManager ;
24+ import com .google .common .base .Strings ;
2425import org .apache .hadoop .hbase .client .*;
2526import org .apache .hadoop .hbase .util .Bytes ;
2627import org .junit .*;
3435
3536import static com .alipay .oceanbase .hbase .util .ObHTableSecondaryPartUtil .*;
3637import static com .alipay .oceanbase .hbase .util .ObHTableTestUtil .FOR_EACH ;
38+ import static com .alipay .oceanbase .hbase .util .ObHTableTestUtil .getConnection ;
3739import static com .alipay .oceanbase .hbase .util .TableTemplateManager .NORMAL_TABLES ;
3840import static org .junit .Assert .*;
3941
@@ -53,7 +55,7 @@ public static void before() throws Exception {
5355 @ AfterClass
5456 public static void finish () throws Exception {
5557 closeDistributedExecute ();
56- dropTables (tableNames , group2tableNames );
58+ // dropTables(tableNames, group2tableNames);
5759 }
5860
5961 @ Before
@@ -68,133 +70,139 @@ private static void assertNullResult(Result result) throws Exception {
6870 private static void testAppend (String tableName ) throws Exception {
6971 OHTableClient hTable = ObHTableTestUtil .newOHTableClient (getTableName (tableName ));
7072 hTable .init ();
73+ try {
74+ byte [] FAMILY = getColumnFamilyName (tableName ).getBytes ();
75+ byte [] ROW = "appendKey" .getBytes ();
76+ byte [] v1 = Bytes .toBytes ("42" );
77+ byte [] v2 = Bytes .toBytes ("23" );
78+ byte [][] QUALIFIERS = new byte [][]{Bytes .toBytes ("b" ), Bytes .toBytes ("a" ),
79+ Bytes .toBytes ("c" )};
80+ Append a = new Append (ROW );
81+ a .add (FAMILY , QUALIFIERS [0 ], v1 );
82+ a .add (FAMILY , QUALIFIERS [1 ], v2 );
83+ a .setReturnResults (false );
84+ assertNullResult (hTable .append (a ));
7185
72- byte [] FAMILY = getColumnFamilyName (tableName ).getBytes ();
73- byte [] ROW = "appendKey" .getBytes ();
74- byte [] v1 = Bytes .toBytes ("42" );
75- byte [] v2 = Bytes .toBytes ("23" );
76- byte [][] QUALIFIERS = new byte [][] { Bytes .toBytes ("b" ), Bytes .toBytes ("a" ),
77- Bytes .toBytes ("c" ) };
78- Append a = new Append (ROW );
79- a .add (FAMILY , QUALIFIERS [0 ], v1 );
80- a .add (FAMILY , QUALIFIERS [1 ], v2 );
81- a .setReturnResults (false );
82- assertNullResult (hTable .append (a ));
83-
84- a = new Append (ROW );
85- a .add (FAMILY , QUALIFIERS [0 ], v2 );
86- a .add (FAMILY , QUALIFIERS [1 ], v1 );
87- a .add (FAMILY , QUALIFIERS [2 ], v2 );
88- Result r = hTable .append (a );
89- assertEquals (0 , Bytes .compareTo (Bytes .add (v1 , v2 ), r .getValue (FAMILY , QUALIFIERS [0 ])));
90- assertEquals (0 , Bytes .compareTo (Bytes .add (v2 , v1 ), r .getValue (FAMILY , QUALIFIERS [1 ])));
91- // QUALIFIERS[2] previously not exist, verify both value and timestamp are correct
92- assertEquals (0 , Bytes .compareTo (v2 , r .getValue (FAMILY , QUALIFIERS [2 ])));
93- assertEquals (r .getColumnLatest (FAMILY , QUALIFIERS [0 ]).getTimestamp (),
94- r .getColumnLatest (FAMILY , QUALIFIERS [2 ]).getTimestamp ());
95-
96- Get get = new Get (ROW );
97- get .setMaxVersions (10 );
98- get .addFamily (FAMILY );
99- Result result = hTable .get (get );
100- assertEquals (2 , result .getColumnCells (FAMILY , QUALIFIERS [0 ]).size ());
101- assertEquals (2 , result .getColumnCells (FAMILY , QUALIFIERS [1 ]).size ());
102- assertEquals (1 , result .getColumnCells (FAMILY , QUALIFIERS [2 ]).size ());
103- assertEquals (
104- 0 ,
105- Bytes .compareTo (Bytes .add (v1 , v2 ), result .getColumnCells (FAMILY , QUALIFIERS [0 ]).get (0 )
106- .getValue ()));
107- assertEquals (0 ,
108- Bytes .compareTo (v2 , result .getColumnCells (FAMILY , QUALIFIERS [2 ]).get (0 ).getValue ()));
86+ a = new Append (ROW );
87+ a .add (FAMILY , QUALIFIERS [0 ], v2 );
88+ a .add (FAMILY , QUALIFIERS [1 ], v1 );
89+ a .add (FAMILY , QUALIFIERS [2 ], v2 );
90+ Result r = hTable .append (a );
91+ assertEquals (0 , Bytes .compareTo (Bytes .add (v1 , v2 ), r .getValue (FAMILY , QUALIFIERS [0 ])));
92+ assertEquals (0 , Bytes .compareTo (Bytes .add (v2 , v1 ), r .getValue (FAMILY , QUALIFIERS [1 ])));
93+ // QUALIFIERS[2] previously not exist, verify both value and timestamp are correct
94+ assertEquals (0 , Bytes .compareTo (v2 , r .getValue (FAMILY , QUALIFIERS [2 ])));
95+ assertEquals (r .getColumnLatest (FAMILY , QUALIFIERS [0 ]).getTimestamp (),
96+ r .getColumnLatest (FAMILY , QUALIFIERS [2 ]).getTimestamp ());
10997
110- hTable .close ();
98+ Get get = new Get (ROW );
99+ get .setMaxVersions (10 );
100+ get .addFamily (FAMILY );
101+ Result result = hTable .get (get );
102+ assertEquals (2 , result .getColumnCells (FAMILY , QUALIFIERS [0 ]).size ());
103+ assertEquals (2 , result .getColumnCells (FAMILY , QUALIFIERS [1 ]).size ());
104+ assertEquals (1 , result .getColumnCells (FAMILY , QUALIFIERS [2 ]).size ());
105+ assertEquals (
106+ 0 ,
107+ Bytes .compareTo (Bytes .add (v1 , v2 ), result .getColumnCells (FAMILY , QUALIFIERS [0 ]).get (0 )
108+ .getValue ()));
109+ assertEquals (0 ,
110+ Bytes .compareTo (v2 , result .getColumnCells (FAMILY , QUALIFIERS [2 ]).get (0 ).getValue ()));
111+ } finally {
112+ hTable .close ();
113+ }
111114 }
112115
113116 private static void testAppendBorder (String tableName ) throws Exception {
114117 OHTableClient hTable = ObHTableTestUtil .newOHTableClient (getTableName (tableName ));
115118 hTable .init ();
116-
117- byte [] FAMILY = getColumnFamilyName (tableName ).getBytes ();
118- byte [] ROW = "appendKey" .getBytes ();
119- byte [] v1 = Bytes .toBytes ("ab" );
120- byte [][] QUALIFIERS = new byte [][] { Bytes .toBytes ("b" ), Bytes .toBytes ("a" ),
121- Bytes .toBytes ("c" ) };
122- Put put = new Put (ROW );
123- put .addColumn (FAMILY , QUALIFIERS [1 ], v1 );
124- hTable .put (put );
125- Append a = new Append (ROW );
126- a .add (FAMILY , QUALIFIERS [1 ], v1 );
127- a .add (FAMILY , QUALIFIERS [2 ], "" .getBytes ());
128- hTable .append (a );
129- Get get = new Get (ROW );
130- get .setMaxVersions (10 );
131- get .addFamily (FAMILY );
132- Result result = hTable .get (get );
133- assertEquals (3 , result .size ());
134-
135- a = new Append (ROW );
136- a .add (FAMILY , QUALIFIERS [2 ], v1 );
137- a .add (FAMILY , QUALIFIERS [2 ], "" .getBytes ());
138- hTable .append (a );
139- get = new Get (ROW );
140- get .setMaxVersions (10 );
141- get .addFamily (FAMILY );
142- result = hTable .get (get );
143- assertEquals (4 , result .size ());
144-
145- byte [] randomBytes = new byte [1025 ];
146- Random random = new Random ();
147- random .nextBytes (randomBytes );
148- a = new Append (ROW );
149- a .add (FAMILY , QUALIFIERS [2 ], randomBytes );
150119 try {
120+ byte [] FAMILY = getColumnFamilyName (tableName ).getBytes ();
121+ byte [] ROW = "appendKey" .getBytes ();
122+ byte [] v1 = Bytes .toBytes ("ab" );
123+ byte [][] QUALIFIERS = new byte [][]{Bytes .toBytes ("b" ), Bytes .toBytes ("a" ),
124+ Bytes .toBytes ("c" )};
125+ Put put = new Put (ROW );
126+ put .addColumn (FAMILY , QUALIFIERS [1 ], v1 );
127+ hTable .put (put );
128+ Append a = new Append (ROW );
129+ a .add (FAMILY , QUALIFIERS [1 ], v1 );
130+ a .add (FAMILY , QUALIFIERS [2 ], "" .getBytes ());
151131 hTable .append (a );
152- fail ("unexpect error, too long data should fail" );
153- } catch (IOException e ) {
154- assertTrue (e .getCause ().getMessage ().contains ("Data too long for column 'V'" ));
155- }
132+ Get get = new Get (ROW );
133+ get .setMaxVersions (10 );
134+ get .addFamily (FAMILY );
135+ Result result = hTable .get (get );
136+ assertEquals (3 , result .size ());
156137
157- hTable .close ();
138+ a = new Append (ROW );
139+ a .add (FAMILY , QUALIFIERS [2 ], v1 );
140+ a .add (FAMILY , QUALIFIERS [2 ], "" .getBytes ());
141+ hTable .append (a );
142+ get = new Get (ROW );
143+ get .setMaxVersions (10 );
144+ get .addFamily (FAMILY );
145+ result = hTable .get (get );
146+ assertEquals (4 , result .size ());
147+
148+ byte [] randomBytes = new byte [1025 ];
149+ Random random = new Random ();
150+ random .nextBytes (randomBytes );
151+ a = new Append (ROW );
152+ a .add (FAMILY , QUALIFIERS [2 ], randomBytes );
153+ try {
154+ hTable .append (a );
155+ fail ("unexpect error, too long data should fail" );
156+ } catch (IOException e ) {
157+ assertTrue (e .getCause ().getMessage ().contains ("Data too long for column 'V'" ));
158+ }
159+ } finally {
160+ hTable .close ();
161+ }
158162
159163 }
160164
161165 private static void testAppendCon (String tableName ) throws Exception {
162166 OHTableClient hTable = ObHTableTestUtil .newOHTableClient (getTableName (tableName ));
163- hTable .init ();
164- byte [] FAMILY = getColumnFamilyName (tableName ).getBytes ();
165- String column = "appColumn" ;
166- byte [] ROW = "appendKey" .getBytes ();
167- byte [] v = "a" .getBytes ();
168- byte [] expect = "a" .getBytes ();
169- ThreadPoolExecutor threadPoolExecutor = OHTable .createDefaultThreadPoolExecutor (1 , 100 ,100 );
170- AtomicInteger atomicInteger = new AtomicInteger (0 );
171- CountDownLatch countDownLatch = new CountDownLatch (100 );
172- for (int i = 0 ; i < 100 ; i ++) {
173- Append append = new Append (ROW );
174- append .add (FAMILY , column .getBytes (), v );
175- threadPoolExecutor .submit (() -> {
176- try {
177- hTable .append (append );
178- atomicInteger .incrementAndGet ();
179- } catch (Exception e ) {
180- if (!e .getCause ().getMessage ().contains ("OB_TRY_LOCK_ROW_CONFLICT" ) && !e .getCause ().getMessage ().contains ("OB_TIMEOUT" )) {
181- throw new RuntimeException (e );
167+ try {
168+ hTable .init ();
169+ byte [] FAMILY = getColumnFamilyName (tableName ).getBytes ();
170+ String column = "appColumn" ;
171+ byte [] ROW = "appendKey" .getBytes ();
172+ byte [] v = "a" .getBytes ();
173+ ThreadPoolExecutor threadPoolExecutor = OHTable .createDefaultThreadPoolExecutor (1 , 100 , 100 );
174+ AtomicInteger atomicInteger = new AtomicInteger (0 );
175+ CountDownLatch countDownLatch = new CountDownLatch (100 );
176+
177+ for (int i = 0 ; i < 100 ; i ++) {
178+ Append append = new Append (ROW );
179+ append .add (FAMILY , column .getBytes (), v );
180+ threadPoolExecutor .submit (() -> {
181+ try {
182+ hTable .append (append );
183+ } catch (Exception e ) {
184+ if (!e .getCause ().getMessage ().contains ("OB_TRY_LOCK_ROW_CONFLICT" )
185+ && !e .getCause ().getMessage ().contains ("OB_TIMEOUT" )) {
186+ throw new RuntimeException (e );
187+ }
188+ } finally {
189+ atomicInteger .incrementAndGet ();
190+ countDownLatch .countDown ();
182191 }
183- } finally {
184- countDownLatch .countDown ();
185- }
186- });
187- }
188- countDownLatch .await (100000 , TimeUnit .MILLISECONDS );
189- for (int i = 0 ; i < atomicInteger .get () - 1 ; i ++) {
190- expect = Bytes .add (expect , v );
192+ });
193+ }
194+ threadPoolExecutor .shutdown ();
195+ countDownLatch .await (100000 , TimeUnit .MILLISECONDS );
196+ final byte [] expect = Strings .repeat ("a" , atomicInteger .get ()).getBytes ();
197+ System .out .println ("atomicInteger: " + atomicInteger .get ());
198+ Get get = new Get (ROW );
199+ get .setMaxVersions (1 );
200+ get .addColumn (FAMILY , column .getBytes ());
201+ Result result = hTable .get (get );
202+ ObHTableTestUtil .Assert (tableName , ()-> assertTrue (0 <= Bytes .compareTo (expect , result .getColumnCells (FAMILY , column .getBytes ()).get (0 ).getValue ())));
203+ } finally {
204+ hTable .close ();
191205 }
192- Get get = new Get (ROW );
193- get .setMaxVersions (1 );
194- get .addColumn (FAMILY , column .getBytes ());
195- Result result = hTable .get (get );
196- assertEquals (0 , Bytes .compareTo (expect , result .getColumnCells (FAMILY , column .getBytes ()).get (0 ).getValue ()));
197- hTable .close ();
198206 }
199207
200208 private static void testAppendMultiCF (Map .Entry <String , List <String >> entry ) throws Exception {
@@ -260,8 +268,8 @@ public void testAppendMultiCF() throws Throwable {
260268
261269 @ Test
262270 public void testAppendSeires () throws Throwable {
263- createTables (TableTemplateManager .TableType .SECONDARY_PARTITIONED_TIME_RANGE_KEY , series_tables , group2tableNames , true );
271+ createTables (TableTemplateManager .TableType .SECONDARY_PARTITIONED_TIME_RANGE_KEY , series_tables , null , true );
264272 FOR_EACH (series_tables , OHTableSecondaryPartAppendTest ::testAppendSeires );
265- dropTables (series_tables , group2tableNames );
273+ dropTables (series_tables , null );
266274 }
267275}
0 commit comments