Skip to content

Commit c01b831

Browse files
committed
NUIDGenerator - refactor init to handle multithreading. The dao loading is lazy and internal to the service, so not protected by normal CSpecFactory logic. Large DAOs can takes minutes to load and some UIDs are shared across DAOs resulting in muiltiple threads requesting 'next' and triggering init
1 parent 0804ea1 commit c01b831

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/foam/util/NUIDGenerator.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ foam.CLASS({
3636
}
3737
3838
AtomicLong seqNo_ = new AtomicLong();
39-
AtomicBoolean initMaxSeqNo_ = new AtomicBoolean(false);
39+
volatile AtomicBoolean initMaxSeqNo_ = new AtomicBoolean(false);
4040
`,
4141

4242
properties: [
@@ -87,18 +87,25 @@ foam.CLASS({
8787
{
8888
name: 'initMaxSeqNo',
8989
javaCode: `
90-
if ( ! initMaxSeqNo_.getAndSet(true) ) {
91-
Logger logger = Loggers.logger(getX(), this);
92-
logger.info(getSalt(), "max", "find");
93-
getDao().select(new AbstractSink() {
94-
@Override
95-
public void put(Object obj, Detachable sub) {
96-
var id = (long) getPropertyInfo().get(obj);
97-
maybeUpdateSeqNo(id);
98-
}
99-
});
100-
Loggers.logger(getX(), this).info(getSalt(), "max", "found", seqNo_.get());
90+
if ( ! initMaxSeqNo_.get() ) {
91+
synchronized ( initMaxSeqNo_ ) {
92+
if ( ! initMaxSeqNo_.get() ) {
93+
Logger logger = Loggers.logger(getX(), this);
94+
logger.info(getSalt(), "max", "find");
95+
getDao().select(new AbstractSink() {
96+
@Override
97+
public void put(Object obj, Detachable sub) {
98+
var id = (long) getPropertyInfo().get(obj);
99+
maybeUpdateSeqNo(id);
100+
}
101+
public void eof() {
102+
initMaxSeqNo_.getAndSet(true);
103+
}
104+
});
105+
Loggers.logger(getX(), this).info(getSalt(), "max", "found", seqNo_.get());
106+
}
101107
}
108+
}
102109
`
103110
},
104111
{

0 commit comments

Comments
 (0)