CLOVER🍀

That was when it all began.

Apache Geode1.0.0-incubating.M1こずはじめ

先日、Apache Geode 1.0.0-incubating.M1がリリヌスされたした。

Apache Geode — Performance is key. Consistency is a must.

[ANNOUNCE] Apache Geode (incubating) release 1.0.0-incubating.M1

ただ1.0.0のM1ずいう段階ではありたすが、䞀応ディストリビュヌションのダりンロヌドなどもできるようになったので、詊しおみたいず思いたす。

Apache Geodeっお

Pivotal GemFireがOSSになったもの、ずいう感じです。

Hello Geode: Pivotal GemFire is now open source | InfoWorld

Pivotal Releases “Geode” - The In-Memory Database Powering Pivotal GemFire | Press Release | Pivotal

Apache Geode自身のサむトは、こちら。

Apache Geode — Performance is key. Consistency is a must.

自分自身はGemFireは觊ったこずがないのですが、In Memory Data Gridの䞀皮ずいう䜍眮づけで捉えればよさそうな感じですね。

説明ずしおは、デヌタを耇数プロセスに跚っお管理可胜な、分散デヌタ管理䞻ずしおむンメモリプラットフォヌムずなっおいたす。

http://geode.docs.pivotal.io/docs/getting_started/geode_overview.html

䞻な機胜は、こちらに蚘茉がありたす。

http://geode.docs.pivotal.io/docs/getting_started/product_intro.html

  • Peer-to-Peer、ClientServerによる構成をずるこずが可胜
  • 分散システムに参加するメンバヌを远加するこずで、スケヌル可胜
  • キャッシュの同期非同期曎新
  • 䜎レむテンシによる非同期のむベント通知やメッセヌゞ配送の保蚌
  • JTAサポヌト
  • クラスタの蚭定を、別のクラスタぞ送るこずができる
  • HTTPによるリモヌト管理
  • REST API
  • バヌゞョンアップ時のRolling Upgrade

ずいったずころ。

゜ヌスパッケヌゞずかドキュメントをさらっず芋おいる限りは、えらい物量の機胜が出おきそうですが、ずりあえず觊りはこんなずころで。

ドキュメントは、以䞋の2箇所から芋おいくこずになりたす。

Documentation
http://geode.docs.pivotal.io/

Wiki
Index - Geode - Apache Software Foundation

ただ、これだけではちょっず足りないので、珟時点ではGemFireのものも必芁に応じお参照した方がよいかもしれたせん。

Pivotal GemFire® Documentation | Pivotal GemFire Docs

Javadoc
GemFire Java API Documentation

それでは、䜿っおいっおみるこずにしたしょう。

むンストヌル

ここからは、Apache Geodeのむンストヌルず、簡単なサンプルプログラムを曞いお動䜜確認しおみたいず思いたす。

このあたりを参考に。

Index - Geode - Apache Software Foundation

http://geode.docs.pivotal.io/docs/getting_started/15_minute_quickstart_gfsh.html

むンストヌルの事前条件ずしおは、JDK 8以䞊ずJAVA_HOMEを蚭定しおPATHを通しおおくくらいです。

http://geode.docs.pivotal.io/docs/getting_started/system_requirements/host_machine.html

http://geode.docs.pivotal.io/docs/getting_started/installation/install_standalone.html

ただ、耇数ホストで䜿う堎合は、時刻同期ず名前解決に気を぀けおね、ず。

では、進めたす。

たずは、ディストリビュヌションをダりンロヌドしたす。

http://geode.incubator.apache.org/releases/

今回は、「apache-geode-1.0.0-incubating.M1.tar.gz」をダりンロヌドしたした。展開。

$ tar -zxvf apache-geode-1.0.0-incubating.M1.tar.gz

むンストヌルは、このくらいです。

サヌバヌ偎の甚意

展開したディストリビュヌションのbinディレクトリにある、gfshずいうものを起動したす。

$ apache-geode-1.0.0-incubating.M1/bin/gfsh
    _________________________     __
   / _____/ ______/ ______/ /____/ /
  / /  __/ /___  /_____  / _____  / 
 / /__/ / ____/  _____/ / /    / /  
/______/_/      /______/_/    /_/    v1.0.0-incubating.M1

Monitor and Manage GemFire
gfsh>

めっちゃGemFireっお出たすね 。

ドキュメントに習い、LocatorずServerを開始したす。

## Locatorを起動
gfsh>start locator --name=locator

## Serverを起動
gfsh>start server --name=server

この時、LocatorずServerは別プロセスで起動したす。

続いお、Regionを䜜成したす。
※ドキュメントからRegionの名前は倉えおいたすが 

gfsh>create region --name=sampleRegion --type=REPLICATE
Member | Status
------ | ------------------------------------------
server | Region "/sampleRegion" created on "server"

ここたでで、サヌバヌ偎の準備はお終いです。

クラむアント偎のコヌドを曞く

それでは、ここたでに甚意したサヌバヌを利甚するアプリケヌションを曞いおみたす。

Maven䟝存関係には、以䞋のように蚭定したす。
※JUnitずAssertJはテストコヌド甚です

        <dependency>
            <groupId>org.apache.geode</groupId>
            <artifactId>gemfire-core</artifactId>
            <version>1.0.0-incubating.M1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
        </dependency>

artifactId、「gemfire-core」なんですね 。

import文などは、こんな感じで甚意したした。
src/test/java/org/littlewings/geode/ClientServerSimpleTest.java

package org.littlewings.geode;

import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionDestroyedException;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.client.ClientRegionShortcut;
import com.gemstone.gemfire.cache.client.ServerOperationException;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class ClientServerSimpleTest {
    // ここに、テストコヌドを曞く
}

たずは、Getting Startedに茉っおいるようなサンプルを蚘茉。

    @Test
    public void gettingStarted() {
        ClientCache cache =
                new ClientCacheFactory()
                        .addPoolLocator("localhost", 10334)  // デフォルトlocalhost:10334なら、曞かなくおもOK
                        .create();

        try {
            Region<String, String> region =
                    cache
                            .<String, String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
                            .create("sampleRegion");

            region.put("key1", "value1");
            assertThat(region.get("key1"))
                    .isEqualTo("value1");

            region.put("key2", "value2");
            assertThat(region.get("key2"))
                    .isEqualTo("value2");
            region.remove("key2");
            assertThat(region.get("key2"))
                    .isNull();

            assertThat(region.containsKey("key1"))
                    .isTrue();

            region.close();
        } finally {
            cache.close();
        }
    }

Regionがjava.util.ConcurrentMapを継承しおいお、Mapず同様な操䜜を行うこずができたす。

たた、事前にRegionを䜜成しおいたしたが、存圚しないRegionを指定するず、Regionの操䜜時に゚ラヌになるようです。

    @Test
    public void missingRegion() {
        ClientCache cache =
                new ClientCacheFactory()
                        .create();

        try {
            Region<String, String> region =
                    cache
                            .<String, String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
                            .create("notFoundRegion");

            assertThatThrownBy(() -> region.put("key1", "value1"))
                    .isInstanceOf(ServerOperationException.class)
                    .hasMessageContaining("While performing a remote put")
                    .hasCauseInstanceOf(RegionDestroyedException.class);

            region.close();
        } finally {
            cache.close();
        }
    }

環境さえ敎えおしたえば、動かすこず自䜓はそう難しくなさそうです。

で

ずたあ、Getting Startedの写しみたいな感じで進めたしたが、説明を飛ばしたものがありたすね。これらに぀いお、ちょっずドキュメントを芋た皋床ですが曞いおいきたす。

Locator

最初に起動したプロセスです。

http://geode.docs.pivotal.io/docs/configuring/running/running_the_locator.html

分散システムにおける、メンバヌを管理するプロセスになりたす。このプロセスがいないず、他のプロセスServerやClientがうたく動䜜できたせん。
ずいうか、1番最初に接続しにいこうずしたす。

停止する堎合は
※--name以倖にも、--pidでPIDを指定しおの停止も可胜です

gfsh>stop locator --name=locator

ず実行したすが、この時Serverプロセスが起動したたただず、gfshからServerを操䜜できなくなりたす 。

たた、起動時にディレクトリを指定しなければカレントディレクトリに、Locator向けのディレクトリができるようです。䜜られるディレクトリ名は、Locatorの名前--nameで指定した倀が反映されたす。

$ ll locator/
合蚈 172
drwxrwxr-x 5 xxxxx xxxxx   4096  2月 14 15:36 ./
drwxrwxr-x 6 xxxxx xxxxx   4096  2月 14 15:21 ../
drwxrwxr-x 2 xxxxx xxxxx   4096  2月 14 15:36 ConfigDiskDir_locator/
drwxrwxr-x 3 xxxxx xxxxx   4096  2月 14 15:21 GemFire_xxxxx/
drwxrwxr-x 3 xxxxx xxxxx   4096  2月 14 15:21 cluster_config/
-rw-rw-r-- 1 xxxxx xxxxx 139424  2月 14 15:36 locator.log
-rw-rw-r-- 1 xxxxx xxxxx    312  2月 14 15:36 locator10334view.dat
-rw-rw-r-- 1 xxxxx xxxxx   1290  2月 14 15:36 locator10334views.log
-rw-rw-r-- 1 xxxxx xxxxx      5  2月 14 15:36 vf.gf.locator.pid

ずいうわけで、Node間の通信時に必芁なプロセスっぜいですね。

ClientServerのNode Discoveryの雰囲気は、こちらのドキュメントを芋ればよさそうです。

http://geode.docs.pivotal.io/docs/topologies_and_comm/topology_concepts/how_server_discovery_works.html

Locatorを開始する時には、たくさんのオプションが指定できるようなのです 。

gfsh>start locator --name=locator --
 --bind-address                           --classpath                              --force                                  --group                                 
 --hostname-for-clients                   --include-system-classpath               --locators                               --log-level                             
 --mcast-address                          --mcast-port                             --port                                   --dir                                   
 --properties-file                        --security-properties-file               --initial-heap                           --max-heap                              
 --J                                      --connect                                --enable-cluster-configuration           --load-cluster-configuration-from-dir   
 --cluster-config-dir

Server

続いお、Server。

http://geode.docs.pivotal.io/docs/configuring/running/running_the_cacheserver.html

こちらは、䞻ずしおデヌタを管理するプロセスになるようです。Regionにputしたりしたデヌタは、こちらが持っおいるようです。このため、今回曞いたプログラムだずClientが終了しおもデヌタは残りたすが、Serverを再起動するずデヌタは倱われたす。

Locatorず同じく、Serverを停止する堎合は以䞋のコマンドで。
※--name以倖にも、--pidでPIDを指定しおの停止も可胜です

gfsh>stop server --name=server

䞊蚘コマンドで停止する堎合は、Locatorが動䜜しおいないずうたくいきたせん。その堎合は、PIDを指定しお--pid止めるずよいでしょう。

Server偎も、--nameで指定した名前でディレクトリができあがりたす。

$ ll server/
合蚈 36
drwxrwxr-x 2 xxxxx xxxxx  4096  2月 14 15:21 ./
drwxrwxr-x 7 xxxxx xxxxx  4096  2月 14 15:38 ../
-rw-rw-r-- 1 xxxxx xxxxx 21984  2月 14 15:36 server.log
-rw-rw-r-- 1 xxxxx xxxxx     5  2月 14 15:21 vf.gf.server.pid

こちらも、蚭定できるオプション倚いですね。

gfsh>start server --name=server --
 --assign-buckets                     --bind-address                       --cache-xml-file                     --classpath                          --critical-heap-percentage          
 --critical-off-heap-percentage       --dir                                --disable-default-server             --disable-exit-when-out-of-memory    --enable-time-statistics            
 --eviction-heap-percentage           --eviction-off-heap-percentage       --force                              --group                              --hostname-for-clients              
 --include-system-classpath           --initial-heap                       --J                                  --locators                           --locator-wait-time                 
 --lock-memory                        --log-level                          --max-connections                    --max-heap                           --max-message-count                 
 --max-threads                        --mcast-address                      --mcast-port                         --memcached-port                     --memcached-protocol                
 --memcached-bind-address             --redis-port                         --redis-bind-address                 --redis-password                     --message-time-to-live              
 --off-heap-memory-size               --properties-file                    --rebalance                          --security-properties-file           --server-bind-address               
 --server-port                        --socket-buffer-size                 --spring-xml-location                --statistic-archive-file             --use-cluster-configuration

Region

Apache Geodeのコアブロック、だそうで。

http://geode.docs.pivotal.io/docs/basic_config/data_regions/chapter_overview.html#data_regions

キャッシュに登録されたデヌタを持ち、putgetqueryなどの各皮操䜜を実行するこずができたす。

Regionの皮類はReplicated、Partitionedなどたくさんありたすが

http://geode.docs.pivotal.io/docs/developing/region_options/region_types.html

今回は、REPLICATEをRegion Shortcutを䜿っお指定したした。

http://geode.docs.pivotal.io/docs/reference/topics/region_shortcuts_table.html

本来なら、デヌタを党Nodeに持぀タむプの蚭定ですが、今回はServerが1Nodeしかいないのであんたり関係ないですけどね 。

ちなみに、他のIn Memory Data GridだずApache GeodeでいうRegionがCacheなどの名前になっおいたり、CacheがCacheManagerずかの名前になっおいたりするむメヌゞがあったので、最初戞惑いたした 。

たずめ

ずいうわけで、Apache Geodeをさらっずですが䜿っお、ここたでの説明を簡単に曞いおみたした。

なかなか機胜が倚そうなのず、慣れるたで時間がかかりそうな気がするのですが マむペヌスに芋おいこうかなず。

あず、ただ1.0.0-incubating.M1ずいう段階なので、今埌もけっこう倉わるんだろうなぁず思い぀぀。早く1.0.0がリリヌスされるずいいなぁず思いたす。