Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NebulaGraph Sessionpool #474

Merged
merged 27 commits into from
Oct 10, 2022
Merged

NebulaGraph Sessionpool #474

merged 27 commits into from
Oct 10, 2022

Conversation

Nicole00
Copy link
Contributor

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Sep 27, 2022

Codecov Report

Base: 63.38% // Head: 62.94% // Decreases project coverage by -0.43% ⚠️

Coverage data is based on head (67d7d4f) compared to base (34b28de).
Patch coverage: 54.68% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #474      +/-   ##
============================================
- Coverage     63.38%   62.94%   -0.44%     
- Complexity      729      778      +49     
============================================
  Files            66       71       +5     
  Lines          3479     3735     +256     
  Branches        507      536      +29     
============================================
+ Hits           2205     2351     +146     
- Misses          928     1015      +87     
- Partials        346      369      +23     
Impacted Files Coverage Δ
...m/vesoft/nebula/client/graph/NebulaPoolConfig.java 100.00% <ø> (ø)
...com/vesoft/nebula/client/graph/net/Connection.java 100.00% <ø> (ø)
...va/com/vesoft/nebula/client/graph/net/Session.java 48.93% <0.00%> (-1.07%) ⬇️
...vesoft/nebula/client/graph/net/SyncConnection.java 50.81% <0.00%> (-0.03%) ⬇️
.../vesoft/nebula/client/graph/SessionPoolConfig.java 46.93% <46.93%> (ø)
...va/com/vesoft/nebula/client/graph/SessionPool.java 56.32% <56.32%> (ø)
.../com/vesoft/nebula/client/graph/NebulaSession.java 68.18% <68.18%> (ø)
...ient/graph/exception/BindSpaceFailedException.java 100.00% <100.00%> (ø)
...m/vesoft/nebula/client/graph/net/SessionState.java 100.00% <100.00%> (ø)
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Nicole00 Nicole00 added the wip label Sep 28, 2022
}
}
// if session size is less than max size, get session from pool
if (sessionList.size() < maxSessionSize) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看没加锁,如果并发走到这里的话,可能会超过最大个数吗?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's lock on the getSession method, so it's impossible to get here concurrently.

*/
private synchronized NebulaSession getSession() throws ClientServerIncompatibleException,
AuthFailedException, IOErrorException, BindSpaceFailedException {
int retry = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请教下哈:这里的retry ,没看到哪里把他设置成0呢,有啥用?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请教下哈:这里的retry ,没看到哪里把他设置成0呢,有啥用?

retry-- : use the value of retry first, then retry minus 1.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的while (retry-- > 0) 是不是只循环了一次,并没有在sleep之后re-get

useSpace(nebulaSession, null);
throw e;
}
useSpace(nebulaSession, resultSet);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useSpace 不用放在final里面么?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only when the code in try{} throws IOErrorException, we need to execute useSpace to release the session.
If it throws AuthFailedException, it means the session is not create successfully.


// test USE SPACE
try {
sessionPool.execute("USE TEST_USE_SPACE");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USE TEST_USE_SPACE这里,可以一个语句中执行多个use space么?比如:

USE SPACE1 xxx;USE SPACE2 xxx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code didn't check this case, so this statement will be executed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想到一种情况哈,如果用户没有这个space的权限,当前内核返回的错误信息能处理吧?--安琪反馈,会在result set中返回

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USE SPACE1 xxx;USE SPACE2 xxx

如果第一个语句报错,第二个语句自动阻塞,不会执行了对吧? 是内核处理的;已验证会报 SpaceNotFound

// NebulaPool init failed
List<HostAddress> addresses = Arrays.asList(new HostAddress(ip, 1000));
SessionPoolConfig config =
new SessionPoolConfig(addresses, "space", "user", "12345");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个失败原因是端口号错误么? 注释中可以明确下不?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I'll add some comments, thanks for your suggestions.

assert (!sessionPool.init());
sessionPool.close();

// set MinSessionSize 0, and init will return true even if the user is wrong

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注释是不是要删掉或改一下?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch


// set MinSessionSize 0, and init will return true even if the user is wrong
config = new SessionPoolConfig(addresses, "space", "user", "nebula")
.setMinSessionSize(0);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最小线程池,如果允许设置成0的话,就说允许session是无效的对吧?貌似也可以

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是按我们最后定的设计来吧

return;
}
}
releaseSession(nebulaSession);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看明白了:
releaseSession是放回去;
nebulaSession.release();是销毁session pool;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nebulaSession.release() is signout.


// test USE SPACE
try {
sessionPool.execute("USE TEST_USE_SPACE");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

想到一种情况哈,如果用户没有这个space的权限,当前内核返回的错误信息能处理吧?--安琪反馈,会在result set中返回

@jinyingsunny jinyingsunny self-requested a review October 10, 2022 03:09
Copy link

@jinyingsunny jinyingsunny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Nicole00 Nicole00 merged commit 96f77e8 into vesoft-inc:master Oct 10, 2022
@abby-cyber abby-cyber self-assigned this Oct 13, 2022
@abby-cyber abby-cyber added the doc affected PR: improvements or additions to documentation label Oct 13, 2022
*/
private synchronized NebulaSession getSession() throws ClientServerIncompatibleException,
AuthFailedException, IOErrorException, BindSpaceFailedException {
int retry = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的while (retry-- > 0) 是不是只循环了一次,并没有在sleep之后re-get


NebulaSession nebulaSession = new NebulaSession(connection, authResult.getSessionId(),
authResult.getTimezoneOffset(), state);
ResultSet result = nebulaSession.execute("USE " + spaceName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"USE " + spaceName 需要加上反引号 `` 么,确保包含特殊字符的spaceName不会报错

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"USE " + spaceName 需要加上反引号 `` 么,确保包含特殊字符的spaceName不会报错

是的,感谢提出

resultSet = nebulaSession.executeWithParameter(stmt, parameterMap);

// re-execute for session error
if (isSessionError(resultSet)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果stmt包含多个执行语句,由于没有事物,其中部分执行成功,部分失败,这里re-execute再次执行会不会造成影响

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果stmt包含多个执行语句,由于没有事物,其中部分执行成功,部分失败,这里re-execute再次执行会不会造成影响

只有发生Session相关的异常才会re-execute,此时语句是没有被执行的

*/
private void checkSession() {
for (NebulaSession nebulaSession : sessionList) {
if (nebulaSession.isIdle()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有没线程安全的问题呢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢提出,pr #479 中将NebulaSession的状态改为AtomicReference 原子变量。

@Override
public boolean ping(long sessionID) {
try {
execute(sessionID, "YIELD 1;");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the sessionID in invalid, return true or false?


// re-execute for session error
if (isSessionError(resultSet)) {
sessionList.remove(nebulaSession);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to drop the connection if the session is invalid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I'll add session.release.

stmtCheck(stmt);
checkSessionPool();
NebulaSession nebulaSession = getSession();
ResultSet resultSet = nebulaSession.getSession().execute(stmt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should try the exception and finally use space

* close the session pool
*/
public void close() {
if (isClosed.get()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it thread safe? i.e. the space is dropped, and two session use space then call use space function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the type of isClosed is thread safe.

/**
* get the number of all Session
*/
public int getSessionNums() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thread safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, the list is thread safe.

throw new IllegalArgumentException("statement is null.");
}

if (stmt.trim().toLowerCase().startsWith("use") && stmt.trim().split(" ").length == 2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use nba

if there're two space, split length is not 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, not judge the use a; use b

@Nicole00 Nicole00 mentioned this pull request Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc affected PR: improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants