TestHashSharding.java
8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/J is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
this software, see the FOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
Floor, Boston, MA 02110-1301 USA
*/
package testsuite.fabric.jdbc;
import java.sql.ResultSet;
import java.sql.Statement;
import com.mysql.fabric.jdbc.FabricMySQLConnection;
import com.mysql.fabric.jdbc.FabricMySQLDataSource;
import testsuite.fabric.BaseFabricTestCase;
/**
* @todo this hash sharding test is incompatible with the
* default Fabric configuration for these C/J Fabric tests.
*/
public class TestHashSharding extends BaseFabricTestCase {
private FabricMySQLDataSource ds;
private FabricMySQLConnection conn;
public TestHashSharding() throws Exception {
super();
if (this.isSetForFabricTest) {
this.ds = getNewDefaultDataSource();
}
}
@Override
public void setUp() throws Exception {
if (this.isSetForFabricTest) {
this.conn = (FabricMySQLConnection) this.ds.getConnection(this.username, this.password);
// create table globally
this.conn.setServerGroupName("fabric_test1_global");
Statement stmt = this.conn.createStatement();
stmt.executeUpdate("drop table if exists employees");
stmt.executeUpdate("create table employees (emp_no INT PRIMARY KEY, first_name CHAR(40), last_name CHAR(40))");
this.conn.clearServerSelectionCriteria();
}
}
@Override
public void tearDown() throws Exception {
if (this.isSetForFabricTest) {
this.conn.close();
}
}
/**
* Run the given query and assert that the result contains at
* least one row.
*/
protected void checkRowExists(String sql) throws Exception {
Statement s = this.conn.createStatement();
ResultSet rs = s.executeQuery(sql);
assertTrue("Row should exist", rs.next());
rs.close();
s.close();
}
/**
* Run the given query and assert that the result set is empty.
*/
protected void checkRowDoesntExist(String sql) throws Exception {
Statement s = this.conn.createStatement();
ResultSet rs = s.executeQuery(sql);
assertFalse("Row should not exist", rs.next());
rs.close();
s.close();
}
/**
* Assert that a row exists in the given server group.
*/
protected void checkRowExistsInServerGroup(String sql, String groupName) throws Exception {
this.conn.clearServerSelectionCriteria();
this.conn.setServerGroupName(groupName);
checkRowExists(sql);
}
/**
* Assert that a row exists in the given shard determined by the key.
*/
protected void checkRowExistsInKeyGroup(String sql, String key) throws Exception {
this.conn.setShardKey(key);
checkRowExists(sql);
}
/**
* Assert that no row exists in the given server group.
*/
protected void checkRowDoesntExistInServerGroup(String sql, String groupName) throws Exception {
this.conn.clearServerSelectionCriteria();
this.conn.setServerGroupName(groupName);
checkRowDoesntExist(sql);
}
/**
* Assert that no row exists in the given shard determined by the key.
*/
public void checkRowDoesntExistInKeyGroup(String sql, String key) throws Exception {
this.conn.setShardKey(key);
checkRowDoesntExist(sql);
}
/**
* Check data used by basic tests is in proper groups.
* Test both by direct group selection and shard table/key selection.
*/
protected void assertBasicDataIsInProperPlaces() throws Exception {
String sql;
sql = "select * from employees where emp_no = 1";
checkRowExistsInServerGroup(sql, "fabric_test1_shard2");
checkRowDoesntExistInServerGroup(sql, "fabric_test1_shard1");
this.conn.clearServerSelectionCriteria();
this.conn.setShardTable("employees");
checkRowExistsInKeyGroup(sql, "1");
checkRowDoesntExistInKeyGroup(sql, "9");
sql = "select * from employees where emp_no = 6"; // repeat with key = 6
checkRowExistsInServerGroup(sql, "fabric_test1_shard2");
checkRowDoesntExistInServerGroup(sql, "fabric_test1_shard1");
this.conn.clearServerSelectionCriteria();
this.conn.setShardTable("employees");
checkRowExistsInKeyGroup(sql, "6");
checkRowDoesntExistInKeyGroup(sql, "19");
sql = "select * from employees where emp_no = 9"; // other shard
checkRowExistsInServerGroup(sql, "fabric_test1_shard1");
checkRowDoesntExistInServerGroup(sql, "fabric_test1_shard2");
this.conn.clearServerSelectionCriteria();
this.conn.setShardTable("employees");
checkRowExistsInKeyGroup(sql, "9");
checkRowDoesntExistInKeyGroup(sql, "6");
sql = "select * from employees where emp_no = 19"; // repeat with key = 19
checkRowExistsInServerGroup(sql, "fabric_test1_shard1");
checkRowDoesntExistInServerGroup(sql, "fabric_test1_shard2");
this.conn.clearServerSelectionCriteria();
this.conn.setShardTable("employees");
checkRowExistsInKeyGroup(sql, "19");
checkRowDoesntExistInKeyGroup(sql, "1");
}
/**
* Basic tests for shard selection with a HASH shard mapping.
*/
public void testBasicInsertByGroup() throws Exception {
if (!this.isSetForFabricTest) {
return;
}
Statement stmt;
// insert data directly by group selection
this.conn.setServerGroupName("fabric_test1_shard2");
stmt = this.conn.createStatement();
stmt.executeUpdate("insert into employees values (1, 'William', 'Gisbon')");
stmt.executeUpdate("insert into employees values (6, 'Samuel', 'Delany')");
this.conn.setServerGroupName("fabric_test1_shard1");
stmt.executeUpdate("insert into employees values (9, 'William', 'Turner')");
stmt.executeUpdate("insert into employees values (19, 'Albrecht', 'Durer')");
// check everything is where it should be
assertBasicDataIsInProperPlaces();
}
/**
* Basic tests for shard selection with a HASH shard mapping.
*/
public void testBasicInsertByKey() throws Exception {
if (!this.isSetForFabricTest) {
return;
}
Statement stmt;
// insert data using shard selection
this.conn.clearServerSelectionCriteria();
this.conn.setShardTable("employees");
stmt = this.conn.createStatement();
this.conn.setShardKey("1"); // hash = c4ca4238a0b923820dcc509a6f75849b
assertEquals("fabric_test1_shard2", this.conn.getCurrentServerGroup().getName());
stmt.executeUpdate("insert into employees values (1, 'William', 'Gisbon')");
this.conn.setShardKey("6"); // hash = 1679091c5a880faf6fb5e6087eb1b2dc
assertEquals("fabric_test1_shard2", this.conn.getCurrentServerGroup().getName());
stmt.executeUpdate("insert into employees values (6, 'Samuel', 'Delany')");
this.conn.setShardKey("9"); // hash = 45c48cce2e2d7fbdea1afc51c7c6ad26
assertEquals("fabric_test1_shard1", this.conn.getCurrentServerGroup().getName());
stmt.executeUpdate("insert into employees values (9, 'William', 'Turner')");
this.conn.setShardKey("19"); // hash = 1f0e3dad99908345f7439f8ffabdffc4
assertEquals("fabric_test1_shard1", this.conn.getCurrentServerGroup().getName());
stmt.executeUpdate("insert into employees values (19, 'Albrecht', 'Durer')");
// check everything is where it should be
assertBasicDataIsInProperPlaces();
}
}