Skip to content

Commit fe76797

Browse files
jeffhostetlerdscho
authored andcommitted
gvfs-helper: add prefetch support
Teach gvfs-helper to support "/gvfs/prefetch" REST API. This includes a new `gvfs-helper prefetch --since=<t>` command line option. And a new `objects.prefetch` verb in `gvfs-helper server` mode. If `since` argument is omitted, `gvfs-helper` will search the local shared-cache for the most recent prefetch packfile and start from there. The <t> is usually a seconds-since-epoch, but may also be a "friendly" date -- such as "midnight", "yesterday" and etc. using the existing date selection mechanism. Add `gh_client__prefetch()` API to allow `git.exe` to easily call prefetch (and using the same long-running process as immediate and queued object fetches). Expanded t5799 unit tests to include prefetch tests. Test setup now also builds some commits-and-trees packfiles for testing purposes with well-known timestamps. Expanded t/helper/test-gvfs-protocol.exe to support "/gvfs/prefetch" REST API. Massive refactor of existing packfile handling in gvfs-helper.c to reuse more code between "/gvfs/objects POST" and "/gvfs/prefetch". With this we now properly name packfiles with the checksum SHA1 rather than a date string. Refactor also addresses some of the confusing tempfile setup and install_<result> code processing (introduced to handle the ambiguity of how POST works with commit objects). Update 2023-05-22 (v2.41.0): add '--no-rev-index' to 'index-pack' to avoid writing the extra (unused) file. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 59438b5 commit fe76797

5 files changed

Lines changed: 1510 additions & 339 deletions

File tree

‎gvfs-helper-client.c‎

Lines changed: 113 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ static struct hashmap gh_server__subprocess_map;
2828
static struct odb_source *gh_client__chosen_odb;
2929

3030
/*
31-
* The "objects" capability has 2 verbs: "get" and "post".
31+
* The "objects" capability has verbs: "get" and "post" and "prefetch".
3232
*/
3333
#define CAP_OBJECTS (1u<<1)
3434
#define CAP_OBJECTS_NAME "objects"
3535

3636
#define CAP_OBJECTS__VERB_GET1_NAME "get"
3737
#define CAP_OBJECTS__VERB_POST_NAME "post"
38+
#define CAP_OBJECTS__VERB_PREFETCH_NAME "prefetch"
3839

3940
static int gh_client__start_fn(struct subprocess_entry *subprocess)
4041
{
@@ -133,6 +134,44 @@ static int gh_client__send__objects_get(struct child_process *process,
133134
return 0;
134135
}
135136

137+
/*
138+
* Send a request to gvfs-helper to prefetch packfiles from either the
139+
* cache-server or the main Git server using "/gvfs/prefetch".
140+
*
141+
* objects.prefetch LF
142+
* [<seconds-since_epoch> LF]
143+
* <flush>
144+
*/
145+
static int gh_client__send__objects_prefetch(struct child_process *process,
146+
timestamp_t seconds_since_epoch)
147+
{
148+
int err;
149+
150+
/*
151+
* We assume that all of the packet_ routines call error()
152+
* so that we don't have to.
153+
*/
154+
155+
err = packet_write_fmt_gently(
156+
process->in,
157+
(CAP_OBJECTS_NAME "." CAP_OBJECTS__VERB_PREFETCH_NAME "\n"));
158+
if (err)
159+
return err;
160+
161+
if (seconds_since_epoch) {
162+
err = packet_write_fmt_gently(process->in, "%" PRItime "\n",
163+
seconds_since_epoch);
164+
if (err)
165+
return err;
166+
}
167+
168+
err = packet_flush_gently(process->in);
169+
if (err)
170+
return err;
171+
172+
return 0;
173+
}
174+
136175
/*
137176
* Update the loose object cache to include the newly created
138177
* object.
@@ -152,7 +191,7 @@ static void gh_client__update_loose_cache(const char *line)
152191
}
153192

154193
/*
155-
* Both CAP_OBJECTS verbs return the same format response:
194+
* CAP_OBJECTS verbs return the same format response:
156195
*
157196
* <odb>
158197
* <data>*
@@ -192,6 +231,8 @@ static int gh_client__objects__receive_response(
192231
const char *v1;
193232
char *line;
194233
int len;
234+
int nr_loose = 0;
235+
int nr_packfile = 0;
195236
int err = 0;
196237

197238
while (1) {
@@ -209,13 +250,13 @@ static int gh_client__objects__receive_response(
209250

210251
else if (starts_with(line, "packfile")) {
211252
ghc |= GHC__CREATED__PACKFILE;
212-
*p_nr_packfile += 1;
253+
nr_packfile++;
213254
}
214255

215256
else if (starts_with(line, "loose")) {
216257
gh_client__update_loose_cache(line);
217258
ghc |= GHC__CREATED__LOOSE;
218-
*p_nr_loose += 1;
259+
nr_loose++;
219260
}
220261

221262
else if (starts_with(line, "ok"))
@@ -234,6 +275,8 @@ static int gh_client__objects__receive_response(
234275
}
235276

236277
*p_ghc = ghc;
278+
*p_nr_loose = nr_loose;
279+
*p_nr_packfile = nr_packfile;
237280

238281
return err;
239282
}
@@ -290,7 +333,7 @@ static struct gh_server__process *gh_client__find_long_running_process(
290333
/*
291334
* Find an existing long-running process with the above command
292335
* line -or- create a new long-running process for this and
293-
* subsequent 'get' requests.
336+
* subsequent requests.
294337
*/
295338
if (!gh_server__subprocess_map_initialized) {
296339
gh_server__subprocess_map_initialized = 1;
@@ -327,10 +370,14 @@ static struct gh_server__process *gh_client__find_long_running_process(
327370

328371
void gh_client__queue_oid(const struct object_id *oid)
329372
{
330-
// TODO consider removing this trace2. it is useful for interactive
331-
// TODO debugging, but may generate way too much noise for a data
332-
// TODO event.
333-
trace2_printf("gh_client__queue_oid: %s", oid_to_hex(oid));
373+
/*
374+
* Keep this trace as a printf only, so that it goes to the
375+
* perf log, but not the event log. It is useful for interactive
376+
* debugging, but generates way too much (unuseful) noise for the
377+
* database.
378+
*/
379+
if (trace2_is_enabled())
380+
trace2_printf("gh_client__queue_oid: %s", oid_to_hex(oid));
334381

335382
if (!oidset_insert(&gh_client__oidset_queued, oid))
336383
gh_client__oidset_count++;
@@ -411,10 +458,14 @@ int gh_client__get_immediate(const struct object_id *oid,
411458
int nr_packfile = 0;
412459
int err = 0;
413460

414-
// TODO consider removing this trace2. it is useful for interactive
415-
// TODO debugging, but may generate way too much noise for a data
416-
// TODO event.
417-
trace2_printf("gh_client__get_immediate: %s", oid_to_hex(oid));
461+
/*
462+
* Keep this trace as a printf only, so that it goes to the
463+
* perf log, but not the event log. It is useful for interactive
464+
* debugging, but generates way too much (unuseful) noise for the
465+
* database.
466+
*/
467+
if (trace2_is_enabled())
468+
trace2_printf("gh_client__get_immediate: %s", oid_to_hex(oid));
418469

419470
entry = gh_client__find_long_running_process(CAP_OBJECTS);
420471
if (!entry)
@@ -443,3 +494,52 @@ int gh_client__get_immediate(const struct object_id *oid,
443494

444495
return err;
445496
}
497+
498+
/*
499+
* Ask gvfs-helper to prefetch commits-and-trees packfiles since a
500+
* given timestamp.
501+
*
502+
* We ignore seconds_since_epoch and use the value from the ODB.
503+
*/
504+
int gh_client__prefetch(timestamp_t seconds_since_epoch UNUSED,
505+
int *nr_packfiles_received)
506+
{
507+
struct gh_server__process *entry;
508+
struct child_process *process;
509+
enum gh_client__created ghc;
510+
int nr_loose = 0;
511+
int nr_packfile = 0;
512+
int err = 0;
513+
514+
entry = gh_client__find_long_running_process(CAP_OBJECTS);
515+
if (!entry)
516+
return -1;
517+
518+
trace2_region_enter("gh-client", "objects/prefetch", the_repository);
519+
520+
process = &entry->subprocess.process;
521+
522+
sigchain_push(SIGPIPE, SIG_IGN);
523+
524+
err = gh_client__send__objects_prefetch(process, /* seconds unknown */ 0);
525+
if (!err)
526+
err = gh_client__objects__receive_response(
527+
process, &ghc, &nr_loose, &nr_packfile);
528+
529+
sigchain_pop(SIGPIPE);
530+
531+
if (err) {
532+
subprocess_stop(&gh_server__subprocess_map,
533+
(struct subprocess_entry *)entry);
534+
FREE_AND_NULL(entry);
535+
}
536+
537+
trace2_data_intmax("gh-client", the_repository,
538+
"prefetch/packfile_count", nr_packfile);
539+
trace2_region_leave("gh-client", "objects/prefetch", the_repository);
540+
541+
if (nr_packfiles_received)
542+
*nr_packfiles_received = nr_packfile;
543+
544+
return err;
545+
}

‎gvfs-helper-client.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,22 @@ void gh_client__queue_oid_array(const struct object_id *oids, int oid_nr);
6666
*/
6767
int gh_client__drain_queue(enum gh_client__created *p_ghc);
6868

69+
/*
70+
* Ask `gvfs-helper server` to fetch any "prefetch packs"
71+
* available on the server more recent than the requested time.
72+
*
73+
* seconds_since_epoch is ignored. the gvfs-helper will scan the ODB for
74+
* the last received prefetch and ask for ones newer than that.
75+
*
76+
* A long-running background process is used to subsequent requests
77+
* (either prefetch or regular immediate/queued requests) more efficient.
78+
*
79+
* One or more packfiles will be created in the shared-cache ODB.
80+
*
81+
* Returns 0 on success, -1 on error. Optionally also returns the
82+
* number of prefetch packs received.
83+
*/
84+
int gh_client__prefetch(timestamp_t seconds_since_epoch,
85+
int *nr_packfiles_received);
86+
6987
#endif /* GVFS_HELPER_CLIENT_H */

0 commit comments

Comments
 (0)