log->data = bluesky_log_map_object(log->fs, -1, log->log_seq,
log->log_offset, log->log_size);
bluesky_cloudlog_stats_update(log, 1);
- } else if (log->location_flags & CLOUDLOG_CLOUD) {
+ }
+
+ if (log->data == NULL && (log->location_flags & CLOUDLOG_CLOUD)) {
+ log->location_flags &= ~CLOUDLOG_JOURNAL;
bluesky_cloudlog_stats_update(log, -1);
log->data = bluesky_log_map_object(log->fs, log->location.directory,
log->location.sequence,
log->location.offset,
log->location.size);
bluesky_cloudlog_stats_update(log, 1);
- } else {
+ }
+
+ if (log->data == NULL) {
g_error("Unable to fetch cloudlog entry!");
}
// Rough size limit for a log segment. This is not a firm limit and there are
// no absolute guarantees on the size of a log segment.
-#define LOG_SEGMENT_SIZE (1 << 24)
+#define LOG_SEGMENT_SIZE (1 << 22)
+
+// Target amount of disk space to use for the journal and cache files, in
+// kilobytes.
+#define DISK_CACHE_SIZE_TARGET (64 * 1024)
#define HEADER_MAGIC 0x676f4c0a
#define FOOTER_MAGIC 0x2e435243
* increment the count of live dirty objects in that journal file. The
* count will be decremented when objects are deleted or written to the
* cloud. */
- g_atomic_int_add(&log->current_log->dirty_refs, 1);
- item->dirty_journal = log->current_log;
+ if (!(item->location_flags & CLOUDLOG_CLOUD)) {
+ g_atomic_int_add(&log->current_log->dirty_refs, 1);
+ item->dirty_journal = log->current_log;
+ }
/* Replace the log item's string data with a memory-mapped copy of the
* data, now that it has been written to the log file. (Even if it
BlueSkyLog *log = fs->log;
+ struct stat statbuf;
char logname[64];
int type;
g_mutex_lock(log->mmap_lock);
map = g_hash_table_lookup(log->mmap_cache, logname);
- if (map == NULL) {
- /* TODO: stat() call */
+ if (map == NULL
+ && type == CLOUDLOG_JOURNAL
+ && fstatat(log->dirfd, logname, &statbuf, 0) < 0) {
+ /* A stale reference to a journal file which doesn't exist any longer
+ * because it was reclaimed. Return NULL. */
+ } else if (map == NULL) {
+ g_print("Adding cache file %s\n", logname);
+
map = g_new0(BlueSkyCacheFile, 1);
- map->type = CLOUDLOG_JOURNAL;
+ map->type = type;
map->lock = g_mutex_new();
map->type = type;
g_mutex_lock(map->lock);
}
g_mutex_unlock(log->mmap_lock);
- g_atomic_int_inc(&map->refcount);
+ if (map != NULL)
+ g_atomic_int_inc(&map->refcount);
return map;
}
g_print(" (fetching)");
g_print("\n");
- if (g_atomic_int_get(&cachefile->refcount) == 0
+ if (g_atomic_int_get(&fs->log->disk_used) > DISK_CACHE_SIZE_TARGET
+ && g_atomic_int_get(&cachefile->refcount) == 0
&& g_atomic_int_get(&cachefile->mapcount) == 0
&& g_atomic_int_get(&cachefile->dirty_refs) == 0)
{
cachefile->filename);
}
+ g_atomic_int_add(&fs->log->disk_used, -(cachefile->len / 1024));
g_hash_table_remove(fs->log->mmap_cache, cachefile->filename);
g_mutex_unlock(cachefile->lock);
g_mutex_free(cachefile->lock);