Exemplo n.º 1
0
func TestSchemaInfoDropTable(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	existingTable := "test_table_01"
	createOrDropTableQuery := fmt.Sprintf("%s and table_name = '%s'", baseShowTables, existingTable)
	db.AddQuery(createOrDropTableQuery, &mproto.QueryResult{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			createTestTableBaseShowTable(existingTable),
		},
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	schemaInfo.Open(&appParams, &dbaParams, getSchemaInfoTestSchemaOverride(), cachePool, false)
	tableInfo := schemaInfo.GetTable(existingTable)
	if tableInfo == nil {
		t.Fatalf("table: %s should exist", existingTable)
	}
	schemaInfo.DropTable(existingTable)
	tableInfo = schemaInfo.GetTable(existingTable)
	if tableInfo != nil {
		t.Fatalf("table: %s should not exist", existingTable)
	}
	schemaInfo.Close()
}
Exemplo n.º 2
0
func TestCachePoolState(t *testing.T) {
	fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, true)
	idleTimeout := 1 * time.Second
	cachePool.idleTimeout = idleTimeout
	cachePool.Open()
	cachePool.memcacheStats.update()
	defer cachePool.Close()
	if cachePool.Available() <= 0 {
		t.Fatalf("cache pool should have connections available")
	}
	if cachePool.Capacity() <= 0 {
		t.Fatalf("cache pool should have positive capacity")
	}
	if cachePool.MaxCap() <= 0 {
		t.Fatalf("cache pool should have positive max cap")
	}
	if cachePool.WaitCount() > 0 {
		t.Fatalf("cache pool has never waited for a connection, WaitCount should return 0")
	}
	if cachePool.WaitTime() > 0 {
		t.Fatalf("cache pool has never waited for a connection, WaitTime should return 0")
	}
	if cachePool.IdleTimeout() != idleTimeout {
		t.Fatalf("cache pool's idle timeout does not match the specified one")
	}
	if len(cachePool.StatsJSON()) <= 0 {
		t.Fatalf("cache pool stats json should return non empty result")
	}
}
Exemplo n.º 3
0
func TestCachePoolStateWithoutOpen(t *testing.T) {
	fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	idleTimeout := 1 * time.Second
	cachePool.idleTimeout = idleTimeout
	if cachePool.StatsJSON() != "{}" {
		t.Fatalf("cache pool StatsJSON() should return {}")
	}
	if cachePool.Capacity() != 0 {
		t.Fatalf("cache pool Capacity() should return 0")
	}
	if cachePool.Available() != 0 {
		t.Fatalf("cache pool Available() should return 0")
	}
	if cachePool.MaxCap() != 0 {
		t.Fatalf("cache pool MaxCap() should return 0")
	}
	if cachePool.WaitCount() != 0 {
		t.Fatalf("cache pool WaitCount() should return 0")
	}
	if cachePool.WaitTime() != 0 {
		t.Fatalf("cache pool WaitTime() should return 0")
	}
	if cachePool.IdleTimeout() != 0 {
		t.Fatalf("cache pool IdleTimeout() should return 0")
	}
	cachePool.Put(nil)
}
Exemplo n.º 4
0
func TestTableInfoWithoutRowCacheViaNoPKColumn(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	db.AddQuery("show index from `test_table`", &sqltypes.Result{})
	db.AddQuery("select * from `test_table` where 1 != 1", &sqltypes.Result{
		Fields: []*querypb.Field{{
			Name: "pk",
			Type: sqltypes.Int32,
		}},
	})
	db.AddQuery("describe `test_table`", &sqltypes.Result{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			{
				sqltypes.MakeString([]byte("pk")),
				sqltypes.MakeString([]byte("int")),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("1")),
				sqltypes.MakeString([]byte{}),
			},
		},
	})

	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db)
	if err != nil {
		t.Fatalf("failed to create a test table info")
	}
	if tableInfo.Cache != nil {
		t.Fatalf("table info's rowcache should be disabled")
	}
}
Exemplo n.º 5
0
func TestSchemaInfoCreateOrUpdateTableFailedDuetoExecErr(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	createOrDropTableQuery := fmt.Sprintf("%s and table_name = '%s'", baseShowTables, "test_table")
	db.AddQuery(createOrDropTableQuery, &mproto.QueryResult{
		// make this query fail
		RowsAffected: math.MaxUint64,
		Rows: [][]sqltypes.Value{
			createTestTableBaseShowTable("test_table"),
		},
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	defer handleAndVerifyTabletError(
		t,
		"CreateOrUpdateTable should fail because it could not tables from MySQL",
		ErrFail,
	)
	schemaInfo.Open(&appParams, &dbaParams, getSchemaInfoTestSchemaOverride(), cachePool, false)
	defer schemaInfo.Close()
	schemaInfo.CreateOrUpdateTable(context.Background(), "test_table")
}
Exemplo n.º 6
0
func TestTableInfoInvalidCardinalityInIndex(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getTestTableInfoQueries() {
		db.AddQuery(query, result)
	}
	db.AddQuery("show index from `test_table`", &sqltypes.Result{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			{
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("PRIMARY")),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("pk")),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("invalid")),
			},
		},
	})
	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db)
	if err != nil {
		t.Fatalf("failed to create a table info: %v", err)
	}
	if len(tableInfo.PKColumns) != 1 {
		t.Fatalf("table should have one PK column although the cardinality is invalid")
	}
}
Exemplo n.º 7
0
func TestSchemaInfoOpenFailedDueToTableInfoErr(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoBaseTestQueries() {
		db.AddQuery(query, result)
	}
	db.AddQuery(baseShowTables, &mproto.QueryResult{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			createTestTableBaseShowTable("test_table"),
		},
	})
	db.AddQuery("describe `test_table`", &mproto.QueryResult{
		// this will cause NewTableInfo error
		RowsAffected: math.MaxUint64,
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	defer handleAndVerifyTabletError(
		t,
		"schema info Open should fail because NewTableInfo failed",
		ErrFail,
	)
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, cachePool, false)
}
Exemplo n.º 8
0
func TestCachePoolStatsURL(t *testing.T) {
	cache := fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	idleTimeout := 1 * time.Second
	cachePool.idleTimeout = idleTimeout
	cachePool.Open()
	request, _ := http.NewRequest("GET", fmt.Sprintf("%sstats", cachePool.statsURL), nil)
	response := httptest.NewRecorder()
	cachePool.ServeHTTP(response, request)
	// any memcache calls should fail
	cache.EnableCacheServiceError()
	response = httptest.NewRecorder()
	cachePool.ServeHTTP(response, request)
	cache.DisableCacheServiceError()
	cachePool.Close()
	response = httptest.NewRecorder()
	cachePool.ServeHTTP(response, request)
	body, _ := ioutil.ReadAll(response.Body)
	matcher := regexp.MustCompile("closed")
	if !matcher.Match(body) {
		t.Fatalf("stats page should contain 'closed', but got %s", string(body))
	}
}
Exemplo n.º 9
0
func TestSchemaInfoCreateOrUpdateTableFailedDuetoExecErr(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	createOrDropTableQuery := fmt.Sprintf("%s and table_name = '%s'", baseShowTables, "test_table")
	db.AddQuery(createOrDropTableQuery, &sqltypes.Result{
		// make this query fail
		RowsAffected: math.MaxUint64,
		Rows: [][]sqltypes.Value{
			createTestTableBaseShowTable("test_table"),
		},
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, true)
	appParams := sqldb.ConnParams{Engine: db.Name}
	dbaParams := sqldb.ConnParams{Engine: db.Name}
	schemaInfo.cachePool.Open()
	defer schemaInfo.cachePool.Close()
	schemaInfo.Open(&appParams, &dbaParams, getSchemaInfoTestSchemaOverride(), false)
	defer schemaInfo.Close()
	originalSchemaErrorCount := schemaInfo.queryServiceStats.InternalErrors.Counts()["Schema"]
	// should silently fail: no errors returned, but increment a counter
	schemaInfo.CreateOrUpdateTable(context.Background(), "test_table")

	newSchemaErrorCount := schemaInfo.queryServiceStats.InternalErrors.Counts()["Schema"]
	schemaErrorDiff := newSchemaErrorCount - originalSchemaErrorCount
	if schemaErrorDiff != 1 {
		t.Errorf("InternalErrors.Schema counter should have increased by 1, instead got %v", schemaErrorDiff)
	}
}
Exemplo n.º 10
0
func TestSchemaInfoGetPlanPanicDuetoEmptyQuery(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	schemaOverrides := getSchemaInfoTestSchemaOverride()
	// test cache type RW
	schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true)
	defer schemaInfo.Close()

	ctx := context.Background()
	logStats := newLogStats("GetPlanStats", ctx)
	defer handleAndVerifyTabletError(
		t,
		"schema info GetPlan should fail because of empty query",
		ErrFail,
	)
	schemaInfo.GetPlan(ctx, logStats, "")
}
Exemplo n.º 11
0
func TestSchemaInfoOpenWithSchemaOverride(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	schemaOverrides := getSchemaInfoTestSchemaOverride()
	// test cache type RW
	schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true)
	testTableInfo := schemaInfo.GetTable("test_table_01")
	if testTableInfo.Table.CacheType != schema.CACHE_RW {
		t.Fatalf("test_table_01's cache type should be RW")
	}
	schemaInfo.Close()
	// test cache type W
	schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true)
	testTableInfo = schemaInfo.GetTable("test_table_02")
	if testTableInfo.Table.CacheType != schema.CACHE_W {
		t.Fatalf("test_table_02's cache type should be W")
	}
	schemaInfo.Close()
}
Exemplo n.º 12
0
func TestTableInfoFailBecauseUnableToRetrieveTableIndex(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getTestTableInfoQueries() {
		db.AddQuery(query, result)
	}
	db.AddRejectedQuery("show index from `test_table`", errRejected)
	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	_, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db)
	if err == nil {
		t.Fatalf("table info creation should fail because it is unable to get test_table index")
	}
}
Exemplo n.º 13
0
func TestTableInfoWithoutRowCacheViaUnknownPKColumnType(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	db.AddQuery("show index from `test_table`", &mproto.QueryResult{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("PRIMARY")),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("pk")),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("300")),
			},
		},
	})
	db.AddQuery("select * from `test_table` where 1 != 1", &mproto.QueryResult{
		Fields: []mproto.Field{{
			Name: "pk",
			Type: mysql.TypeNewDecimal,
		}},
	})
	db.AddQuery("describe `test_table`", &mproto.QueryResult{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.MakeString([]byte("pk")),
				sqltypes.MakeString([]byte("decimal")),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte{}),
				sqltypes.MakeString([]byte("1")),
				sqltypes.MakeString([]byte{}),
			},
		},
	})

	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db)
	if err != nil {
		t.Fatalf("failed to create a test table info")
	}
	if tableInfo.Cache != nil {
		t.Fatalf("table info's rowcache should be disabled")
	}
}
Exemplo n.º 14
0
func TestCachePoolOpenWithInvalidBinary(t *testing.T) {
	fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "invalid_binary",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	defer func() {
		if e := recover(); e == nil {
			t.Fatalf("open a cache pool with an invalid rowCacheConfig.Binary should panic")
		}
	}()
	cachePool.Open()
	cachePool.Close()
}
Exemplo n.º 15
0
func TestCachePoolFailToStartBecauseCacheServiceWasDown(t *testing.T) {
	cache := fakecacheservice.Register()
	fakesqldb.Register()
	testUtils := &testUtils{}
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	idleTimeout := 1 * time.Second
	cachePool.idleTimeout = idleTimeout
	// any memcache calls should fail
	cache.EnableCacheServiceError()
	defer testUtils.checkTabletErrorWithRecover(t, ErrFatal, "can't communicate with cache service")
	cachePool.Open()
}
Exemplo n.º 16
0
func TestCachePoolOpenTwice(t *testing.T) {
	fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	cachePool.Open()
	defer cachePool.Close()
	defer func() {
		if e := recover(); e == nil {
			t.Fatalf("open an opened cache pool should panic")
		}
	}()
	cachePool.Open()
}
Exemplo n.º 17
0
func TestSchemaInfoExportVars(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, true)
	appParams := sqldb.ConnParams{Engine: db.Name}
	dbaParams := sqldb.ConnParams{Engine: db.Name}
	schemaInfo.cachePool.Open()
	defer schemaInfo.cachePool.Close()
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, true)
	defer schemaInfo.Close()
	expvar.Do(func(kv expvar.KeyValue) {
		_ = kv.Value.String()
	})
}
Exemplo n.º 18
0
func TestTableInfoWithoutRowCacheViaTableType(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getTestTableInfoQueries() {
		db.AddQuery(query, result)
	}
	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	tableInfo, err := newTestTableInfo(cachePool, "VIEW", "test table", db)
	if err != nil {
		t.Fatalf("failed to create a test table info")
	}
	if tableInfo.Cache != nil {
		t.Fatalf("table info's rowcache should be disabled")
	}
}
Exemplo n.º 19
0
func TestCachePoolGetFailedBecauseCachePoolIsClosed(t *testing.T) {
	fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	idleTimeout := 1 * time.Second
	cachePool.idleTimeout = idleTimeout
	ctx := context.Background()
	defer func() {
		if err := recover(); err == nil {
			t.Fatalf("Get should fail because cache pool is closed")
		}
	}()
	cachePool.Get(ctx)
}
Exemplo n.º 20
0
func TestCachePool(t *testing.T) {
	fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, false)
	if !cachePool.IsClosed() {
		t.Fatalf("cache pool is not closed")
	}
	cachePool.Open()
	if cachePool.IsClosed() {
		t.Fatalf("cache pool is closed")
	}
	cachePool.Close()
	if !cachePool.IsClosed() {
		t.Fatalf("cache pool is not closed")
	}
}
Exemplo n.º 21
0
func TestTableInfoWithoutRowCacheViaComment(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getTestTableInfoQueries() {
		db.AddQuery(query, result)
	}
	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "vitess_nocache", db)
	if err != nil {
		t.Fatalf("failed to create a test table info")
	}
	if tableInfo.Cache != nil {
		t.Fatalf("table info's rowcache should be disabled")
	}
	if tableInfo.StatsJSON() != "null" {
		t.Fatalf("rowcache is disabled, stats should be null")
	}
}
Exemplo n.º 22
0
func TestSchemaInfoStrictMode(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoBaseTestQueries() {
		db.AddQuery(query, result)
	}
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	defer handleAndVerifyTabletError(
		t,
		"schema info Open should fail because of underlying "+
			"connection cannot verify strict mode",
		ErrFatal,
	)
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, cachePool, true)
}
Exemplo n.º 23
0
func TestSchemaInfoStatsURL(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	query := "select * from test_table_01"
	db.AddQuery("select * from test_table_01 where 1 != 1", &mproto.QueryResult{})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(false, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, cachePool, true)
	defer schemaInfo.Close()
	// warm up cache
	ctx := context.Background()
	logStats := newLogStats("GetPlanStats", ctx)
	schemaInfo.GetPlan(ctx, logStats, query)

	request, _ := http.NewRequest("GET", schemaInfo.endpoints[debugQueryPlansKey], nil)
	response := httptest.NewRecorder()
	schemaInfo.ServeHTTP(response, request)

	request, _ = http.NewRequest("GET", schemaInfo.endpoints[debugQueryStatsKey], nil)
	response = httptest.NewRecorder()
	schemaInfo.ServeHTTP(response, request)

	request, _ = http.NewRequest("GET", schemaInfo.endpoints[debugTableStatsKey], nil)
	response = httptest.NewRecorder()
	schemaInfo.ServeHTTP(response, request)

	request, _ = http.NewRequest("GET", schemaInfo.endpoints[debugSchemaKey], nil)
	response = httptest.NewRecorder()
	schemaInfo.ServeHTTP(response, request)

	request, _ = http.NewRequest("GET", "/debug/unknown", nil)
	response = httptest.NewRecorder()
	schemaInfo.ServeHTTP(response, request)
}
Exemplo n.º 24
0
func TestCachePoolMemcacheStatsFail(t *testing.T) {
	cache := fakecacheservice.Register()
	fakesqldb.Register()
	rowCacheConfig := RowCacheConfig{
		Binary:      "ls",
		Connections: 100,
	}
	cachePool := newTestCachePool(rowCacheConfig, true)
	idleTimeout := 1 * time.Second
	cachePool.idleTimeout = idleTimeout
	cachePool.Open()
	defer cachePool.Close()
	memcacheStatsBefore := cachePool.queryServiceStats.InternalErrors.Counts()["MemcacheStats"]
	// any memcache calls should fail
	cache.EnableCacheServiceError()
	cachePool.memcacheStats.update()
	memcacheStatsAfter := cachePool.queryServiceStats.InternalErrors.Counts()["MemcacheStats"]
	if memcacheStatsAfter <= memcacheStatsBefore {
		t.Fatalf("memcache stats should cause an internal error")
	}
}
Exemplo n.º 25
0
func TestTableInfoNew(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getTestTableInfoQueries() {
		db.AddQuery(query, result)
	}
	cachePool := newTestTableInfoCachePool()
	cachePool.Open()
	defer cachePool.Close()
	tableInfo, err := newTestTableInfo(cachePool, "USER_TABLE", "test table", db)
	if err != nil {
		t.Fatalf("failed to create a test table info")
	}
	if tableInfo.Cache == nil {
		t.Fatalf("rowcache should be enabled")
	}
	stats := tableInfo.StatsJSON()
	if stats == "" || stats == "null" {
		t.Fatalf("rowcache is enabled, stats should not be empty or null")
	}
}
Exemplo n.º 26
0
func TestSchemaInfoOpenFailedDueToInvalidTimeFormat(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	db.AddQuery("select unix_timestamp()", &mproto.QueryResult{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			// make safety check fail, invalid time format
			[]sqltypes.Value{sqltypes.MakeString([]byte("invalid_time"))},
		},
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{Engine: db.Name}
	dbaParams := sqldb.ConnParams{Engine: db.Name}
	schemaInfo.cachePool.Open()
	defer schemaInfo.cachePool.Close()
	defer handleAndVerifyTabletError(
		t,
		"schema info Open should fail because it could not get MySQL time",
		ErrFail,
	)
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, false)
}
Exemplo n.º 27
0
func TestSchemaInfoQueryCache(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}

	firstQuery := "select * from test_table_01"
	secondQuery := "select * from test_table_02"
	db.AddQuery("select * from test_table_01 where 1 != 1", &mproto.QueryResult{})
	db.AddQuery("select * from test_table_02 where 1 != 1", &mproto.QueryResult{})

	schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, true)
	appParams := sqldb.ConnParams{}
	dbaParams := sqldb.ConnParams{}
	cachePool := newTestSchemaInfoCachePool(true, schemaInfo.queryServiceStats)
	cachePool.Open()
	defer cachePool.Close()
	schemaOverrides := getSchemaInfoTestSchemaOverride()
	// test cache type RW
	schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, cachePool, true)
	defer schemaInfo.Close()

	ctx := context.Background()
	logStats := newLogStats("GetPlanStats", ctx)
	schemaInfo.SetQueryCacheSize(1)
	firstPlan := schemaInfo.GetPlan(ctx, logStats, firstQuery)
	if firstPlan == nil {
		t.Fatalf("plan should not be nil")
	}
	secondPlan := schemaInfo.GetPlan(ctx, logStats, secondQuery)
	if secondPlan == nil {
		t.Fatalf("plan should not be nil")
	}
	expvar.Do(func(kv expvar.KeyValue) {
		_ = kv.Value.String()
	})
	schemaInfo.ClearQueryPlanCache()
}
Exemplo n.º 28
0
func TestSchemaInfoQueryCacheFailDueToInvalidCacheSize(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	for query, result := range getSchemaInfoTestSupportedQueries() {
		db.AddQuery(query, result)
	}
	schemaInfo := newTestSchemaInfo(10, 10*time.Second, 10*time.Second, false)
	appParams := sqldb.ConnParams{Engine: db.Name}
	dbaParams := sqldb.ConnParams{Engine: db.Name}
	schemaInfo.cachePool.Open()
	defer schemaInfo.cachePool.Close()
	schemaOverrides := getSchemaInfoTestSchemaOverride()
	// test cache type RW
	schemaInfo.Open(&appParams, &dbaParams, schemaOverrides, true)
	defer schemaInfo.Close()
	defer handleAndVerifyTabletError(
		t,
		"schema info SetQueryCacheSize should use a positive size",
		ErrFail,
	)
	schemaInfo.SetQueryCacheCap(0)
}
Exemplo n.º 29
0
func TestSchemaInfoOpenFailedDueToMissMySQLTime(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	db.AddQuery("select unix_timestamp()", &mproto.QueryResult{
		// make this query fail
		RowsAffected: math.MaxUint64,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{sqltypes.MakeString([]byte("1427325875"))},
		},
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{Engine: db.Name}
	dbaParams := sqldb.ConnParams{Engine: db.Name}
	schemaInfo.cachePool.Open()
	defer schemaInfo.cachePool.Close()
	defer handleAndVerifyTabletError(
		t,
		"schema info Open should fail because of it could not get MySQL time",
		ErrFail,
	)
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, false)
}
Exemplo n.º 30
0
func TestSchemaInfoOpenFailedDueToIncorrectMysqlRowNum(t *testing.T) {
	fakecacheservice.Register()
	db := fakesqldb.Register()
	db.AddQuery("select unix_timestamp()", &mproto.QueryResult{
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			// make this query fail
			nil,
		},
	})
	schemaInfo := newTestSchemaInfo(10, 1*time.Second, 1*time.Second, false)
	appParams := sqldb.ConnParams{Engine: db.Name}
	dbaParams := sqldb.ConnParams{Engine: db.Name}
	schemaInfo.cachePool.Open()
	defer schemaInfo.cachePool.Close()
	defer handleAndVerifyTabletError(
		t,
		"schema info Open should fail because of incorrect MySQL row number",
		ErrFail,
	)
	schemaInfo.Open(&appParams, &dbaParams, []SchemaOverride{}, false)
}