// GetPlan returns the ExecPlan that for the query. Plans are cached in a cache.LRUCache. func (si *SchemaInfo) GetPlan(ctx context.Context, logStats *LogStats, sql string) *ExecPlan { span := trace.NewSpanFromContext(ctx) span.StartLocal("SchemaInfo.GetPlan") defer span.Finish() // Fastpath if plan already exists. if plan := si.getQuery(sql); plan != nil { return plan } // TODO(sougou): It's not correct to hold this lock here because the code // below runs queries against MySQL. But if we don't hold the lock, there // are other race conditions where identical queries will end up building // plans and compete with populating the query cache. In other words, we // need a more elaborate scheme that blocks less, but still prevents these // race conditions. si.mu.Lock() defer si.mu.Unlock() // Recheck. A plan might have been built by someone else. if plan := si.getQuery(sql); plan != nil { return plan } var tableInfo *TableInfo GetTable := func(tableName string) (table *schema.Table, ok bool) { tableInfo, ok = si.tables[tableName] if !ok { return nil, false } return tableInfo.Table, true } splan, err := planbuilder.GetExecPlan(sql, GetTable) if err != nil { panic(PrefixTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err, "")) } plan := &ExecPlan{ExecPlan: splan, TableInfo: tableInfo} plan.Rules = si.queryRuleSources.filterByPlan(sql, plan.PlanID, plan.TableName) plan.Authorized = tableacl.Authorized(plan.TableName, plan.PlanID.MinRole()) if plan.PlanID.IsSelect() { if plan.FieldQuery == nil { log.Warningf("Cannot cache field info: %s", sql) } else { conn := getOrPanic(ctx, si.connPool) defer conn.Recycle() sql := plan.FieldQuery.Query start := time.Now() r, err := conn.Exec(ctx, sql, 1, true) logStats.AddRewrittenSQL(sql, start) if err != nil { panic(PrefixTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err, "Error fetching fields: ")) } plan.Fields = r.Fields } } else if plan.PlanID == planbuilder.PlanDDL || plan.PlanID == planbuilder.PlanSet { return plan } si.queries.Set(sql, plan) return plan }
func (si *SchemaInfo) GetPlan(logStats *SQLQueryStats, sql string) (plan *ExecPlan) { // Fastpath if plan already exists. if plan := si.getQuery(sql); plan != nil { return plan } si.mu.Lock() defer si.mu.Unlock() // Recheck. A plan might have been built by someone else. if plan := si.getQuery(sql); plan != nil { return plan } var tableInfo *TableInfo GetTable := func(tableName string) (table *schema.Table, ok bool) { tableInfo, ok = si.tables[tableName] if !ok { return nil, false } return tableInfo.Table, true } splan, err := planbuilder.GetExecPlan(sql, GetTable) if err != nil { panic(NewTabletError(FAIL, "%s", err)) } plan = &ExecPlan{ExecPlan: splan, TableInfo: tableInfo} plan.Rules = si.rules.filterByPlan(sql, plan.PlanId, plan.TableName) plan.Authorized = tableacl.Authorized(plan.TableName, plan.PlanId.MinRole()) if plan.PlanId.IsSelect() { if plan.FieldQuery == nil { log.Warningf("Cannot cache field info: %s", sql) } else { conn := getOrPanic(si.connPool) defer conn.Recycle() sql := plan.FieldQuery.Query r, err := conn.ExecuteFetch(sql, 1, true) logStats.QuerySources |= QUERY_SOURCE_MYSQL logStats.NumberOfQueries += 1 logStats.AddRewrittenSql(sql) if err != nil { panic(NewTabletError(FAIL, "Error fetching fields: %v", err)) } plan.Fields = r.Fields } } else if plan.PlanId == planbuilder.PLAN_DDL || plan.PlanId == planbuilder.PLAN_SET { return plan } si.queries.Set(sql, plan) return plan }
// GetPlan returns the ExecPlan that for the query. Plans are cached in a cache.LRUCache. func (si *SchemaInfo) GetPlan(ctx context.Context, logStats *SQLQueryStats, sql string) *ExecPlan { // Fastpath if plan already exists. if plan := si.getQuery(sql); plan != nil { return plan } si.mu.Lock() defer si.mu.Unlock() // Recheck. A plan might have been built by someone else. if plan := si.getQuery(sql); plan != nil { return plan } var tableInfo *TableInfo GetTable := func(tableName string) (table *schema.Table, ok bool) { tableInfo, ok = si.tables[tableName] if !ok { return nil, false } return tableInfo.Table, true } splan, err := planbuilder.GetExecPlan(sql, GetTable) if err != nil { panic(PrefixTabletError(ErrFail, vtrpc.ErrorCode_UNKNOWN_ERROR, err, "")) } plan := &ExecPlan{ExecPlan: splan, TableInfo: tableInfo} plan.Rules = QueryRuleSources.filterByPlan(sql, plan.PlanId, plan.TableName) plan.Authorized = tableacl.Authorized(plan.TableName, plan.PlanId.MinRole()) if plan.PlanId.IsSelect() { if plan.FieldQuery == nil { log.Warningf("Cannot cache field info: %s", sql) } else { conn := getOrPanic(ctx, si.connPool) defer conn.Recycle() sql := plan.FieldQuery.Query start := time.Now() r, err := conn.Exec(ctx, sql, 1, true) logStats.AddRewrittenSql(sql, start) if err != nil { panic(PrefixTabletError(ErrFail, vtrpc.ErrorCode_UNKNOWN_ERROR, err, "Error fetching fields: ")) } plan.Fields = r.Fields } } else if plan.PlanId == planbuilder.PLAN_DDL || plan.PlanId == planbuilder.PLAN_SET { return plan } si.queries.Set(sql, plan) return plan }