2016-04-25 65 views
0

我認爲terracotta bigmemory可以很容易地解決數據一致性問題,但它需要ehcache.xml和源代碼中的幾個參數/屬性,因爲我在其文檔上閱讀它。Terracotta BigMemory共享數據不一致

我ehcache.xml中是:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" 
     name="config"> 

    <cache name="bigMemory" 
     maxBytesLocalHeap="128M" 
     copyOnRead="true" 
     copyOnWrite="true" 
     eternal="true"> 

    <terracotta consistency="strong" /> 
    </cache> 

    <terracottaConfig url="localhost:9510" rejoin="false"/> 

</ehcache> 

和代碼段讀取,並增加對共享數據現在值:

for (int i = 0; i < 1000; i++) { 
      transactionController.begin(); 
      bigMemoryChip.put(new Element(uid, ((Long) bigMemoryChip.get(uid).getObjectValue())+1)); 
      transactionController.commit(); 
     } 

我所做的是兩次執行代碼,並觀察它是如何處理一致性的,通常我所期望的是最終值比初始值高2000。

儘管我嘗試了大約15次,但只有一次比初始值高出2000倍,但其他所有的大約比初始值大1500-1700。

回答

0

不知道你的transactionController是,但除非它是一個羣集的數據結構中提供的排除,您所執行的操作不是原子,因此你看到putget之間的競爭。

也就是說有可能是put發生此節點上getput之間的其他節點,最終將躲藏增量上。

由於您正在使用強一致性,如果您在條件replace中更改put,測試將通過。

+0

transactionController屬於緩存對象。據文件記載,開始和提交方法將保證原子性。和'是',一致性很強。 (見上面的.xml)@louis_jacomet – bAris

+0

好的,你能指出你是如何得到它的?這可能意味着您正在嘗試使用本地事務而不配置它們。所以並不感到驚訝,它不能按預期工作。 –