Skip to content

Commit 79b0c84

Browse files
Fixing some compile issues for alpine and centos, also fix endian issue for FLASH hashslot prefix (#722)
*fix for rocksdb centos compile fail * fix alpine compile * fix hashslot enumerate * update alpine docker to latest version * fix mac build, use big endian for expire prefix * use endian.h for expires too
1 parent 7049b5f commit 79b0c84

7 files changed

Lines changed: 35 additions & 17 deletions

File tree

pkg/docker/Dockerfile_Alpine

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.12
1+
FROM alpine:3.18
22
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
33
RUN addgroup -S -g 1000 keydb && adduser -S -G keydb -u 999 keydb
44
RUN mkdir -p /etc/keydb

src/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ endif
358358
OS := $(shell cat /etc/os-release | grep ID= | head -n 1 | cut -d'=' -f2)
359359
ifeq ($(OS),alpine)
360360
FINAL_CXXFLAGS+=-DUNW_LOCAL_ONLY
361+
FINAL_CXXFLAGS+=-DALPINE
361362
FINAL_LIBS += -lunwind
362363
endif
363364

src/readwritelock.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class readWriteLock {
4242
while (m_readCount > 0)
4343
m_cv.wait(rm);
4444
if (exclusive) {
45-
/* Another thread might have the write lock while we have the read lock
46-
but won't be able to release it until they can acquire the read lock
47-
so release the read lock and try again instead of waiting to avoid deadlock */
45+
/* Another thread might have the write lock while we have the internal lock
46+
but won't be able to release it until they can acquire the internal lock
47+
so release the internal lock and try again instead of waiting to avoid deadlock */
4848
while(!m_writeLock.try_lock())
4949
m_cv.wait(rm);
5050
}

src/server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3923,7 +3923,7 @@ void updateActiveReplicaMastersFromRsi(rdbSaveInfo *rsi);
39233923
uint64_t getMvccTstamp();
39243924
void incrementMvccTstamp();
39253925

3926-
#if __GNUC__ >= 7 && !defined(NO_DEPRECATE_FREE)
3926+
#if __GNUC__ >= 7 && !defined(NO_DEPRECATE_FREE) && !defined(ALPINE)
39273927
[[deprecated]]
39283928
void *calloc(size_t count, size_t size) noexcept;
39293929
[[deprecated]]

src/storage/rocksdb.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "../cluster.h"
88
#include "rocksdbfactor_internal.h"
99

10+
template class std::basic_string<char>;
11+
1012
static const char keyprefix[] = INTERNAL_KEY_PREFIX;
1113

1214
rocksdb::Options DefaultRocksDBOptions();
@@ -24,8 +26,9 @@ bool FInternalKey(const char *key, size_t cch)
2426

2527
std::string getPrefix(unsigned int hashslot)
2628
{
27-
char *hash_char = (char *)&hashslot;
28-
return std::string(hash_char + (sizeof(unsigned int) - 2), 2);
29+
HASHSLOT_PREFIX_TYPE slot = HASHSLOT_PREFIX_ENDIAN((HASHSLOT_PREFIX_TYPE)hashslot);
30+
char *hash_char = (char *)&slot;
31+
return std::string(hash_char, HASHSLOT_PREFIX_BYTES);
2932
}
3033

3134
std::string prefixKey(const char *key, size_t cchKey)
@@ -184,7 +187,7 @@ bool RocksDBStorageProvider::enumerate(callback fn) const
184187
if (FInternalKey(it->key().data(), it->key().size()))
185188
continue;
186189
++count;
187-
bool fContinue = fn(it->key().data()+2, it->key().size()-2, it->value().data(), it->value().size());
190+
bool fContinue = fn(it->key().data()+HASHSLOT_PREFIX_BYTES, it->key().size()-HASHSLOT_PREFIX_BYTES, it->value().data(), it->value().size());
188191
if (!fContinue)
189192
break;
190193
}
@@ -203,17 +206,17 @@ bool RocksDBStorageProvider::enumerate_hashslot(callback fn, unsigned int hashsl
203206
std::string prefix = getPrefix(hashslot);
204207
std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spcolfamily.get()));
205208
size_t count = 0;
206-
for (it->Seek(prefix.c_str()); it->Valid(); it->Next()) {
209+
for (it->Seek(prefix); it->Valid(); it->Next()) {
207210
if (FInternalKey(it->key().data(), it->key().size()))
208211
continue;
209-
if (strncmp(it->key().data(),prefix.c_str(),2) != 0)
212+
if (HASHSLOT_PREFIX_RECOVER(*(HASHSLOT_PREFIX_TYPE *)it->key().data()) != hashslot)
210213
break;
211214
++count;
212-
bool fContinue = fn(it->key().data()+2, it->key().size()-2, it->value().data(), it->value().size());
215+
bool fContinue = fn(it->key().data()+HASHSLOT_PREFIX_BYTES, it->key().size()-HASHSLOT_PREFIX_BYTES, it->value().data(), it->value().size());
213216
if (!fContinue)
214217
break;
215218
}
216-
bool full_iter = !it->Valid() || (strncmp(it->key().data(),prefix.c_str(),2) != 0);
219+
bool full_iter = !it->Valid() || (HASHSLOT_PREFIX_RECOVER(*(HASHSLOT_PREFIX_TYPE *)it->key().data()) != hashslot);
217220
if (full_iter && count != g_pserver->cluster->slots_keys_count[hashslot])
218221
{
219222
printf("WARNING: rocksdb hashslot count mismatch");
@@ -227,7 +230,8 @@ void RocksDBStorageProvider::setExpire(const char *key, size_t cchKey, long long
227230
{
228231
rocksdb::Status status;
229232
std::unique_lock<fastlock> l(m_lock);
230-
std::string prefix((const char *)&expire,sizeof(long long));
233+
long long beExpire = htobe64(expire);
234+
std::string prefix((const char *)&beExpire,sizeof(long long));
231235
std::string strKey(key, cchKey);
232236
if (m_spbatch != nullptr)
233237
status = m_spbatch->Put(m_spexpirecolfamily.get(), rocksdb::Slice(prefix + strKey), rocksdb::Slice(strKey));
@@ -241,7 +245,8 @@ void RocksDBStorageProvider::removeExpire(const char *key, size_t cchKey, long l
241245
{
242246
rocksdb::Status status;
243247
std::unique_lock<fastlock> l(m_lock);
244-
std::string prefix((const char *)&expire,sizeof(long long));
248+
long long beExpire = htobe64(expire);
249+
std::string prefix((const char *)&beExpire,sizeof(long long));
245250
std::string strKey(key, cchKey);
246251
std::string fullKey = prefix + strKey;
247252
if (!FExpireExists(fullKey))
@@ -278,7 +283,7 @@ std::vector<std::string> RocksDBStorageProvider::getEvictionCandidates(unsigned
278283
for (it->Seek(randomHashSlot()); it->Valid() && result.size() < count; it->Next()) {
279284
if (FInternalKey(it->key().data(), it->key().size()))
280285
continue;
281-
result.emplace_back(it->key().data() + 2, it->key().size() - 2);
286+
result.emplace_back(it->key().data() + HASHSLOT_PREFIX_BYTES, it->key().size() - HASHSLOT_PREFIX_BYTES);
282287
}
283288
} else {
284289
std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator(ReadOptions(), m_spexpirecolfamily.get()));

src/storage/rocksdb.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44
#include "../IStorage.h"
55
#include <rocksdb/db.h>
66
#include <rocksdb/utilities/write_batch_with_index.h>
7+
#ifdef __APPLE__
8+
#include <libkern/OSByteOrder.h>
9+
#define htole16 OSSwapHostToLittleInt16
10+
#define le16toh OSSwapLittleToHostInt16
11+
#define htobe64 OSSwapHostToBigInt64
12+
#else
13+
#include <endian.h>
14+
#endif
715
#include "../fastlock.h"
816

917
#define INTERNAL_KEY_PREFIX "\x00\x04\x03\x00\x05\x02\x04"
18+
#define HASHSLOT_PREFIX_TYPE uint16_t
19+
#define HASHSLOT_PREFIX_BYTES sizeof(HASHSLOT_PREFIX_TYPE)
20+
#define HASHSLOT_PREFIX_ENDIAN htole16
21+
#define HASHSLOT_PREFIX_RECOVER le16toh
1022
static const char count_key[] = INTERNAL_KEY_PREFIX "__keydb__count\1";
1123
static const char version_key[] = INTERNAL_KEY_PREFIX "__keydb__version\1";
1224
static const char meta_key[] = INTERNAL_KEY_PREFIX "__keydb__metadata\1";

src/storage/rocksdbfactory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ RocksDBStorageFactory::RocksDBStorageFactory(const char *dbfile, int dbnum, cons
7272
rocksdb::DB *db = nullptr;
7373

7474
auto options = RocksDbOptions();
75-
options.prefix_extractor.reset(rocksdb::NewFixedPrefixTransform(2));
75+
options.prefix_extractor.reset(rocksdb::NewFixedPrefixTransform(HASHSLOT_PREFIX_BYTES));
7676

7777
for (int idb = 0; idb < dbnum; ++idb)
7878
{
@@ -196,7 +196,7 @@ IStorage *RocksDBStorageFactory::create(int db, key_load_iterator iter, void *pr
196196
printf("\tDatabase %d was not shutdown cleanly, recomputing metrics\n", db);
197197
fFirstRealKey = false;
198198
if (iter != nullptr)
199-
iter(it->key().data()+2, it->key().size()-2, privdata);
199+
iter(it->key().data()+HASHSLOT_PREFIX_BYTES, it->key().size()-HASHSLOT_PREFIX_BYTES, privdata);
200200
++count;
201201
}
202202
}

0 commit comments

Comments
 (0)