Skip to content

Commit

Permalink
fix(cache operation): swallow cache errors and other improvements (#1515
Browse files Browse the repository at this point in the history
)
  • Loading branch information
StarpTech authored Jan 19, 2025
1 parent ee89aaa commit d959e2c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 10 deletions.
12 changes: 12 additions & 0 deletions controlplane/src/core/bufservices/analytics/getOperationContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
import type { RouterOptions } from '../../routes.js';
import { enrichLogger, getLogger, handleError } from '../../util.js';

// Get operation content by hash
// TODO: Specify daterange to improve clickhouse performance
export function getOperationContent(
opts: RouterOptions,
req: GetOperationContentRequest,
Expand Down Expand Up @@ -47,6 +49,16 @@ export function getOperationContent(
};
}

if (result.length === 0) {
return {
response: {
code: EnumStatusCode.ERR_NOT_FOUND,
details: 'Requested operation not found',
},
operationContent: '',
};
}

return {
response: {
code: EnumStatusCode.OK,
Expand Down
12 changes: 7 additions & 5 deletions controlplane/src/core/repositories/CacheWarmerRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface DBCacheWarmerOperation {
content?: string;
hash?: string;
name?: string;
persistedID?: string;
persistedId?: string;
clientName?: string;
clientVersion?: string;
planningTime?: number;
Expand All @@ -49,6 +49,7 @@ export class CacheWarmerRepository {
const parsedDateRange = isoDateRangeToTimestamps(dateRange, rangeInHours);
const [start, end] = getDateRange(parsedDateRange);
const quantile = 0.9;
const minPlanningTimeInMs = 0;

const query = `
WITH
Expand All @@ -58,8 +59,8 @@ export class CacheWarmerRepository {
OperationHash as operationHash,
OperationName as operationName,
OperationPersistedID as operationPersistedID,
ClientName as clientName,
ClientVersion as clientVersion,
if(ClientName = 'unknown', '', ClientName) as clientName,
if(ClientVersion = 'missing', '', ClientVersion) as clientVersion,
func_rank(${quantile}, BucketCounts) as rank,
func_rank_bucket_lower_index(rank, BucketCounts) as b,
round(func_histogram_v2(
Expand All @@ -75,6 +76,7 @@ export class CacheWarmerRepository {
AND OrganizationID = '${organizationId}'
AND OperationName != 'IntrospectionQuery'
GROUP BY OperationHash, OperationName, OperationPersistedID, ClientName, ClientVersion
HAVING planningTime > ${minPlanningTimeInMs}
ORDER BY planningTime DESC LIMIT 100
`;

Expand Down Expand Up @@ -235,7 +237,7 @@ export class CacheWarmerRepository {
dbCacheWarmerOperations.push({
name: operation.operationName,
hash: operation.operationHash,
persistedID: operation.operationPersistedID,
persistedId: operation.operationPersistedID,
clientName: operation.clientName,
clientVersion: operation.clientVersion,
planningTime: operation.planningTime,
Expand All @@ -253,7 +255,7 @@ export class CacheWarmerRepository {
content: operationContent,
name: operation.operationName,
hash: operation.operationHash,
persistedID: operation.operationPersistedID,
persistedId: operation.operationPersistedID,
clientName: operation.clientName,
clientVersion: operation.clientVersion,
planningTime: operation.planningTime,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/wg/cosmo/graphqlmetrics/v1/graphqlmetrics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ message RequestInfo {
}

message SchemaUsageInfo {
// RequestDocument is the GraphQL request document
// RequestDocument is the fully normalized GraphQL request document
string RequestDocument = 1;
// TypeFieldMetrics is the list of used fields in the request document
repeated TypeFieldUsageInfo TypeFieldMetrics = 2;
Expand Down
3 changes: 2 additions & 1 deletion router/core/graph_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,8 @@ func (s *graphServer) buildGraphMux(ctx context.Context,

err = WarmupCaches(ctx, warmupConfig)
if err != nil {
return nil, fmt.Errorf("failed to warmup caches: %w", err)
// We don't want to fail the server if the cache warmup fails
s.logger.Error("Failed to warmup caches. It will retry after server restart or graph execution config update", zap.Error(err))
}
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion studio/src/components/cache/cache-details-sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ export const CacheOperationDetails = ({
code={operationContent}
language="graphql"
disableLinking
className="scrollbar-custom w-3/6 overflow-auto"
prettyPrint={false}
className="scrollbar-custom overflow-auto"
/>

<div className="px-2 py-2">
Expand Down
6 changes: 6 additions & 0 deletions studio/src/components/code-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ export const CodeViewer = ({
code,
disableLinking,
className,
prettyPrint = true,
language = "graphql",
}: {
code: string;
disableLinking?: boolean;
className?: string;
prettyPrint?: boolean;
language?: "graphql" | "json";
}) => {
const router = useRouter();
Expand All @@ -99,6 +101,10 @@ export const CodeViewer = ({
useEffect(() => {
const set = async (source: string) => {
try {
if (!prettyPrint) {
setContent(source);
return;
}
const res = await prettier.format(source, {
parser: language,
plugins: [graphQLPlugin, estreePlugin, babelPlugin],
Expand Down

0 comments on commit d959e2c

Please sign in to comment.