You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hbase.apache.org by Guilherme Germoglio <ge...@gmail.com> on 2010/09/08 16:33:52 UTC

missing sync in HTablePool.getTable()

Hello,

I think I came across a small (and mostly harmless) problem in
HTablePool.getTable() code:

1   public HTableInterface getTable(String tableName) {
2     LinkedList<HTableInterface> queue = tables.get(tableName);
3     if(queue == null) {
4       queue = new LinkedList<HTableInterface>();
5       tables.put(tableName, queue);
6       return createHTable(tableName);
7     }
8     HTableInterface table;
9     synchronized(queue) {
10      table = queue.poll();
11    }
12    if(table == null) {
13      return createHTable(tableName);
14    }
15    return table;
16  }

Considering that this method is invoked by multiple threads, it may happen
that while 'queue == null' is true in line 3, it is possible to have a queue
mapped to that name into the tables map when line 5 is executed.

However, I don't think it will cause any harm because the overwritten queue
will eventually be garbage-collected. :-)

Should I write a patch for it?

If so, I considered two approaches:

1. sync the *tables* attribute from lines 3 to 7.
2. or consider the interface ConcurrentMap so we could use putIfAbsent()

I prefer the second. What do you think?

Thanks,

Guilherme

Re: missing sync in HTablePool.getTable()

Posted by Stack <st...@duboce.net>.
On Wed, Sep 8, 2010 at 7:33 AM, Guilherme Germoglio <ge...@gmail.com> wrote:
> However, I don't think it will cause any harm because the overwritten queue
> will eventually be garbage-collected. :-)
>
> Should I write a patch for it?
>

I agree its harmless looking but if you have a moment, please write a patch.

> If so, I considered two approaches:
>
> 1. sync the *tables* attribute from lines 3 to 7.
> 2. or consider the interface ConcurrentMap so we could use putIfAbsent()
>
> I prefer the second. What do you think?
>

Yes.  Me too.  Change tables to be a concurrent Map type instead of
the wrap to make it synchronized.

Thanks G,
St.Ack