-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from nacos-group/develop
Bugfix #70 , to comply with Nacos 2.x, the client won't check instanceId got from the server and will automatically create an instanceId when registering a new service Testcase order adjust to avoid test failures
- Loading branch information
Showing
20 changed files
with
221 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
#include "NacosString.h" | ||
#include "NacosExceptions.h" | ||
#include "thread/AtomicInt.h" | ||
#include "src/thread/Mutex.h" | ||
#include "src/config/IOUtils.h" | ||
|
||
namespace nacos | ||
{ | ||
|
||
template<typename T> | ||
class SequenceProvider { | ||
private: | ||
NacosString _fileName; | ||
AtomicInt<T> _current; | ||
Mutex _acquireMutex; | ||
T _nr_to_preserve; | ||
T _initSequence; | ||
volatile T _hwm;//high water mark | ||
|
||
void ensureWrite(int fd, T data) { | ||
size_t bytes_written = 0; | ||
while (bytes_written < sizeof(T)) { | ||
bytes_written += write(fd, (char*)&data + bytes_written, sizeof(T) - bytes_written); | ||
} | ||
} | ||
|
||
T preserve() { | ||
T current; | ||
int fd; | ||
bool newFile = false; | ||
if (IOUtils::checkNotExistOrNotFile(_fileName)) { | ||
newFile = true; | ||
} | ||
mode_t mode = S_IRUSR | S_IWUSR | S_IRWXG | S_IWGRP; | ||
fd = open(_fileName.c_str(), O_RDWR | O_CREAT, mode); | ||
if (fd <= 0) { | ||
throw new NacosException(NacosException::UNABLE_TO_OPEN_FILE, _fileName); | ||
} | ||
|
||
if (newFile) { | ||
ensureWrite(fd, _initSequence); | ||
lseek(fd, 0, SEEK_SET);//read from the beginning | ||
} | ||
|
||
size_t bytes_read = 0; | ||
while (bytes_read < sizeof(T)) | ||
{ | ||
bytes_read += read(fd, (char*)¤t + bytes_read, sizeof(T) - bytes_read); | ||
} | ||
lseek(fd, 0, SEEK_SET);//write from the beginning | ||
|
||
ensureWrite(fd, current + _nr_to_preserve); | ||
close(fd); | ||
_hwm = current + _nr_to_preserve; | ||
return current; | ||
}; | ||
public: | ||
SequenceProvider(const NacosString &fileName, T initSequence, T nr_to_preserve) { | ||
_fileName = fileName; | ||
_initSequence = initSequence; | ||
_nr_to_preserve = nr_to_preserve; | ||
_current.set(preserve()); | ||
}; | ||
|
||
T next(int step = 1) { | ||
T res = _current.getAndInc(step); | ||
while (res >= _hwm) { | ||
_acquireMutex.lock(); | ||
if (res >= _hwm) { | ||
preserve(); | ||
} | ||
_acquireMutex.unlock(); | ||
} | ||
return res; | ||
}; | ||
}; | ||
|
||
} // namespace nacos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.