Skip to content

Commit

Permalink
Respect numShards in create (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinjc authored Nov 20, 2018
1 parent 0b4f851 commit 0285b2c
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/query/api/v1/handler/database/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ func defaultedPlacementInitRequest(
return nil, errInvalidDBType
}

numShardsInput := r.GetNumShards()
if numShardsInput > 0 {
numShards = numShardsInput
}

return &admin.PlacementInitRequest{
NumShards: numShards,
ReplicationFactor: replicationFactor,
Expand Down
100 changes: 100 additions & 0 deletions src/query/api/v1/handler/database/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,106 @@ func TestLocalType(t *testing.T) {
xtest.Diff(mustPrettyJSON(t, expectedResponse), mustPrettyJSON(t, string(body))))
}

func TestLocalTypeWithNumShards(t *testing.T) {
mockClient, mockKV, mockPlacementService := SetupDatabaseTest(t)
createHandler := NewCreateHandler(mockClient, config.Configuration{}, testDBCfg)
w := httptest.NewRecorder()

jsonInput := `
{
"namespaceName": "testNamespace",
"type": "local",
"numShards": 51
}
`

req := httptest.NewRequest("POST", "/database/create", strings.NewReader(jsonInput))
require.NotNil(t, req)

mockKV.EXPECT().Get(namespace.M3DBNodeNamespacesKey).Return(nil, kv.ErrNotFound)
mockKV.EXPECT().CheckAndSet(namespace.M3DBNodeNamespacesKey, gomock.Any(), gomock.Not(nil)).Return(1, nil)

placementProto := &placementpb.Placement{
Instances: map[string]*placementpb.Instance{
"localhost": &placementpb.Instance{
Id: "m3db_local",
IsolationGroup: "local",
Zone: "embedded",
Weight: 1,
Endpoint: "http://localhost:9000",
Hostname: "localhost",
Port: 9000,
},
},
}
newPlacement, err := placement.NewPlacementFromProto(placementProto)
require.NoError(t, err)
mockPlacementService.EXPECT().BuildInitialPlacement(gomock.Any(), 51, 1).Return(newPlacement, nil)

createHandler.ServeHTTP(w, req)

resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

expectedResponse := `
{
"namespace": {
"registry": {
"namespaces": {
"testNamespace": {
"bootstrapEnabled": true,
"flushEnabled": true,
"writesToCommitLog": true,
"cleanupEnabled": true,
"repairEnabled": false,
"retentionOptions": {
"retentionPeriodNanos": "86400000000000",
"blockSizeNanos": "3600000000000",
"bufferFutureNanos": "120000000000",
"bufferPastNanos": "600000000000",
"blockDataExpiry": true,
"blockDataExpiryAfterNotAccessPeriodNanos": "300000000000"
},
"snapshotEnabled": false,
"indexOptions": {
"enabled": true,
"blockSizeNanos": "3600000000000"
}
}
}
}
},
"placement": {
"placement": {
"instances": {
"m3db_local": {
"id": "m3db_local",
"isolationGroup": "local",
"zone": "embedded",
"weight": 1,
"endpoint": "http://localhost:9000",
"shards": [],
"shardSetId": 0,
"hostname": "localhost",
"port": 9000
}
},
"replicaFactor": 0,
"numShards": 0,
"isSharded": false,
"cutoverTime": "0",
"isMirrored": false,
"maxShardSetId": 0
},
"version": 0
}
}
`
assert.Equal(t, stripAllWhitespace(expectedResponse), string(body),
xtest.Diff(mustPrettyJSON(t, expectedResponse), mustPrettyJSON(t, string(body))))
}
func TestLocalWithBlockSizeNanos(t *testing.T) {
mockClient, mockKV, mockPlacementService := SetupDatabaseTest(t)
createHandler := NewCreateHandler(mockClient, config.Configuration{}, testDBCfg)
Expand Down

0 comments on commit 0285b2c

Please sign in to comment.