├── .gitignore
├── LICENSE
├── README.md
├── appengine-modules-ear
├── build.gradle
├── nbactions.xml
├── pom.xml
└── src
│ └── main
│ └── application
│ └── META-INF
│ ├── MANIFEST.MF
│ ├── appengine-application.xml
│ ├── application.xml
│ └── maven-application.xml
├── appengine-modules-guestbook
├── build.gradle
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── google
│ │ │ └── appengine
│ │ │ └── demos
│ │ │ └── guestbook
│ │ │ ├── GuestbookServlet.java
│ │ │ └── SignGuestbookServlet.java
│ └── webapp
│ │ ├── WEB-INF
│ │ ├── appengine-web.xml
│ │ ├── datastore-indexes.xml
│ │ ├── logging.properties
│ │ └── web.xml
│ │ ├── guestbook.jsp
│ │ └── stylesheets
│ │ └── main.css
│ └── test
│ └── java
│ └── com
│ └── google
│ └── appengine
│ └── demos
│ └── guestbook
│ ├── GuestbookServletTest.java
│ └── SignGuestbookServletTest.java
├── appengine-modules-shardedcounter
├── build.gradle
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── com
│ │ └── google
│ │ └── appengine
│ │ └── demos
│ │ └── shardedcounter
│ │ ├── java
│ │ ├── v1
│ │ │ ├── CounterPage.java
│ │ │ └── ShardedCounter.java
│ │ └── v2
│ │ │ ├── CounterPage.java
│ │ │ └── ShardedCounter.java
│ │ └── jdo
│ │ ├── PMF.java
│ │ ├── v1
│ │ ├── Counter.java
│ │ ├── CounterPage.java
│ │ └── LimitedCounterShard.java
│ │ ├── v2
│ │ ├── Counter.java
│ │ ├── CounterFactory.java
│ │ ├── CounterPage.java
│ │ ├── CounterShard.java
│ │ └── ShardedCounter.java
│ │ └── v3
│ │ ├── CounterFactory.java
│ │ ├── CounterPage.java
│ │ ├── DatastoreCounter.java
│ │ ├── DatastoreCounterShard.java
│ │ └── ShardedCounter.java
│ ├── resources
│ └── META-INF
│ │ └── jdoconfig.xml
│ └── webapp
│ ├── WEB-INF
│ ├── appengine-web.xml
│ └── web.xml
│ ├── favicon.ico
│ └── index.html
├── build.gradle
├── pom.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | build/
3 | .gradle
4 | datanucleus.log
5 | .DS_Store
6 | out
7 | *.iml
8 | *.ipr
9 | *.iws
10 | .idea
11 | .settings
12 | .classpath
13 | .project
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | This project is no longer actively developed or maintained.
4 |
5 | For new work on this check out [java-docs-samples](https://github.com/GoogleCloudPlatform/java-docs-samples) and [getting-started-java](https://github.com/GoogleCloudPlatform/getting-started-java).
6 |
7 |
8 | # appengine-modules-sample-java
9 |
10 |
11 | It has 2 web apps each deployed to a different module: guestbook and shardedcounter.
12 |
13 | Usage (After changing the app id in the appengine-modules-ear/src/main/application/META-INF/appengine-application.xml):
14 |
15 | git clone https://github.com/GoogleCloudPlatform/appengine-modules-sample-java.git
16 | cd appengine-modules-sample-java
17 | mvn install
18 | cd appengine-modules-ear
19 | #to test it locally:
20 | mvn appengine:devserver
21 | #or to deploy it:
22 | mvn appengine:update
23 |
24 | =============================
25 |
--------------------------------------------------------------------------------
/appengine-modules-ear/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'ear'
2 | apply plugin: 'appengine'
3 |
4 | buildscript {
5 | repositories {
6 | mavenCentral()
7 | }
8 |
9 | dependencies {
10 | classpath 'com.google.appengine:gradle-appengine-plugin:1.8.6'
11 | }
12 | }
13 |
14 | dependencies {
15 | appengineSdk 'com.google.appengine:appengine-java-sdk:1.8.6'
16 | deploy project(path: ':appengine-modules-guestbook', configuration: 'archives')
17 | deploy project(path: ':appengine-modules-shardedcounter', configuration: 'archives')
18 | }
19 |
20 | appengine {
21 | downloadSdk = true
22 | }
23 |
--------------------------------------------------------------------------------
/appengine-modules-ear/nbactions.xml:
--------------------------------------------------------------------------------
1 |
2 |
Hello, ${fn:escapeXml(user.nickname)}! (You can 34 | sign out.)
35 | <% 36 | } else { 37 | %> 38 |Hello! 39 | Sign in 40 | to include your name with greetings you post.
41 | <% 42 | } 43 | %> 44 | 45 | <% 46 | final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 47 | final Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName); 48 | // Run an ancestor query to ensure we see the most up-to-date 49 | // view of the Greetings belonging to the selected Guestbook. 50 | final Query query = new Query("Greeting", guestbookKey).addSort("date", Query.SortDirection.DESCENDING); 51 | final ListGuestbook '${fn:escapeXml(guestbookName)}' has no messages.
55 | <% 56 | } else { 57 | %> 58 |Messages in Guestbook '${fn:escapeXml(guestbookName)}'.
59 | <% 60 | for (final Entity greeting : greetings) { 61 | pageContext.setAttribute("greeting_content", 62 | greeting.getProperty("content")); 63 | if (greeting.getProperty("user") == null) { 64 | %> 65 |An anonymous person wrote:
66 | <% 67 | } else { 68 | pageContext.setAttribute("greeting_user", 69 | greeting.getProperty("user")); 70 | %> 71 |${fn:escapeXml(greeting_user.nickname)} wrote:
72 | <% 73 | } 74 | %> 75 |${fn:escapeXml(greeting_content)}76 | <% 77 | } 78 | } 79 | %> 80 | 81 | 86 | 87 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/main/webapp/stylesheets/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Verdana, Helvetica, sans-serif; 3 | background-color: #FFFFCC; 4 | } 5 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/test/java/com/google/appengine/demos/guestbook/GuestbookServletTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.appengine.demos.guestbook; 18 | 19 | import static junit.framework.Assert.assertEquals; 20 | import static org.mockito.Mockito.mock; 21 | import static org.mockito.Mockito.when; 22 | 23 | import com.google.appengine.api.users.User; 24 | import com.google.appengine.api.users.UserServiceFactory; 25 | import com.google.appengine.tools.development.testing.LocalServiceTestHelper; 26 | import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig; 27 | 28 | import org.junit.After; 29 | import org.junit.Before; 30 | import org.junit.Test; 31 | 32 | import java.io.IOException; 33 | import java.io.PrintWriter; 34 | import java.io.StringWriter; 35 | 36 | import javax.servlet.http.HttpServletRequest; 37 | import javax.servlet.http.HttpServletResponse; 38 | 39 | public class GuestbookServletTest { 40 | 41 | private GuestbookServlet guestbookServlet; 42 | 43 | private final LocalServiceTestHelper helper = 44 | new LocalServiceTestHelper(new LocalUserServiceTestConfig()) 45 | .setEnvIsLoggedIn(true) 46 | .setEnvAuthDomain("localhost") 47 | .setEnvEmail("test@localhost"); 48 | 49 | @Before 50 | public void setupGuestBookServlet() { 51 | helper.setUp(); 52 | guestbookServlet = new GuestbookServlet(); 53 | } 54 | 55 | @After 56 | public void tearDownHelper() { 57 | helper.tearDown(); 58 | } 59 | 60 | @Test 61 | public void testDoGet() throws IOException { 62 | final HttpServletRequest request = mock(HttpServletRequest.class); 63 | final HttpServletResponse response = mock(HttpServletResponse.class); 64 | 65 | final StringWriter stringWriter = new StringWriter(); 66 | 67 | when(response.getWriter()).thenReturn(new PrintWriter(stringWriter)); 68 | 69 | guestbookServlet.doGet(request, response); 70 | 71 | final User currentUser = UserServiceFactory.getUserService().getCurrentUser(); 72 | 73 | assertEquals("Hello, " + currentUser.getNickname() + "\n", stringWriter.toString()); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /appengine-modules-guestbook/src/test/java/com/google/appengine/demos/guestbook/SignGuestbookServletTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2012 Google Inc. All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.google.appengine.demos.guestbook; 18 | 19 | import static junit.framework.Assert.assertEquals; 20 | import static junit.framework.Assert.assertTrue; 21 | import static org.mockito.Mockito.mock; 22 | import static org.mockito.Mockito.verify; 23 | import static org.mockito.Mockito.when; 24 | 25 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 26 | import com.google.appengine.api.datastore.Entity; 27 | import com.google.appengine.api.datastore.EntityNotFoundException; 28 | import com.google.appengine.api.datastore.Query; 29 | import com.google.appengine.api.users.User; 30 | import com.google.appengine.api.users.UserServiceFactory; 31 | import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; 32 | import com.google.appengine.tools.development.testing.LocalServiceTestHelper; 33 | 34 | import org.junit.After; 35 | import org.junit.Before; 36 | import org.junit.Test; 37 | 38 | import java.io.IOException; 39 | import java.util.Date; 40 | 41 | import javax.servlet.http.HttpServletRequest; 42 | import javax.servlet.http.HttpServletResponse; 43 | 44 | public class SignGuestbookServletTest { 45 | 46 | private SignGuestbookServlet signGuestbookServlet; 47 | 48 | private final LocalServiceTestHelper helper = 49 | new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()) 50 | .setEnvIsLoggedIn(true) 51 | .setEnvAuthDomain("localhost") 52 | .setEnvEmail("test@localhost"); 53 | 54 | @Before 55 | public void setupSignGuestBookServlet() { 56 | helper.setUp(); 57 | signGuestbookServlet = new SignGuestbookServlet(); 58 | } 59 | 60 | @After 61 | public void tearDownHelper() { 62 | helper.tearDown(); 63 | } 64 | 65 | @Test 66 | public void testDoPost() throws IOException, EntityNotFoundException { 67 | final HttpServletRequest request = mock(HttpServletRequest.class); 68 | final HttpServletResponse response = mock(HttpServletResponse.class); 69 | 70 | final String guestbookName = "TestGuestbook"; 71 | final String testContent = "Test Content"; 72 | 73 | when(request.getParameter("guestbookName")).thenReturn(guestbookName); 74 | when(request.getParameter("content")).thenReturn(testContent); 75 | 76 | final Date priorToRequest = new Date(); 77 | 78 | signGuestbookServlet.doPost(request, response); 79 | 80 | final Date afterRequest = new Date(); 81 | 82 | verify(response).sendRedirect("/guestbook.jsp?guestbookName=TestGuestbook"); 83 | 84 | final User currentUser = UserServiceFactory.getUserService().getCurrentUser(); 85 | 86 | final Entity greeting = DatastoreServiceFactory.getDatastoreService().prepare(new Query()).asSingleEntity(); 87 | 88 | assertEquals(guestbookName, greeting.getKey().getParent().getName()); 89 | assertEquals(testContent, greeting.getProperty("content")); 90 | assertEquals(currentUser, greeting.getProperty("user")); 91 | 92 | final Date date = (Date) greeting.getProperty("date"); 93 | assertTrue("The date in the entity [" + date + "] is prior to the request being performed", 94 | priorToRequest.before(date) || priorToRequest.equals(date)); 95 | assertTrue("The date in the entity [" + date + "] is after to the request completed", 96 | afterRequest.after(date) || afterRequest.equals(date)); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'war' 2 | 3 | dependencies { 4 | compile 'com.google.appengine:appengine-api-1.0-sdk:1.8.5' 5 | compile 'javax.servlet:servlet-api:2.5' 6 | compile 'javax.jdo:jdo-api:2.5' 7 | compile 'net.sf.jsr107cache:jsr107cache:1.1' 8 | compile 'org.datanucleus:datanucleus-core:3.1.4' 9 | compile 'com.google.appengine.orm:datanucleus-appengine:2.1.2' 10 | compile 'org.datanucleus:datanucleus-api-jdo:3.1.3' 11 | testCompile 'com.google.appengine:appengine-testing:1.8.5' 12 | testCompile 'com.google.appengine:appengine-api-stubs:1.8.5' 13 | } 14 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/pom.xml: -------------------------------------------------------------------------------- 1 | 2 |
Current count: " + counter.getCount() + "
"); 37 | 38 | resp.getWriter().println(""); 41 | 42 | resp.getWriter().println(" "); 43 | resp.getWriter().println(""); 44 | } 45 | 46 | @Override 47 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 48 | throws IOException { 49 | final ShardedCounter counter = new ShardedCounter(); 50 | counter.increment(); 51 | doGet(req, resp); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v1/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v1; 17 | 18 | import com.google.appengine.api.datastore.DatastoreService; 19 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 20 | import com.google.appengine.api.datastore.Entity; 21 | import com.google.appengine.api.datastore.EntityNotFoundException; 22 | import com.google.appengine.api.datastore.Key; 23 | import com.google.appengine.api.datastore.KeyFactory; 24 | import com.google.appengine.api.datastore.Query; 25 | import com.google.appengine.api.datastore.Transaction; 26 | 27 | import java.util.Random; 28 | 29 | /** 30 | * This initial implementation simply counts all instances of the 31 | * SimpleCounterShard kind in the datastore. The only way to increment the 32 | * number of shards is to add another shard by creating another entity in the 33 | * datastore. 34 | */ 35 | public class ShardedCounter { 36 | 37 | private static final DatastoreService ds = DatastoreServiceFactory 38 | .getDatastoreService(); 39 | 40 | /** 41 | * Default number of shards. 42 | */ 43 | private static final int NUM_SHARDS = 20; 44 | 45 | /** 46 | * A random number generator, for distributing writes across shards. 47 | */ 48 | private final Random generator = new Random(); 49 | 50 | /** 51 | * Retrieve the value of this sharded counter. 52 | * 53 | * @return Summed total of all shards' counts 54 | */ 55 | public long getCount() { 56 | long sum = 0; 57 | 58 | final Query query = new Query("SimpleCounterShard"); 59 | for (final Entity e : ds.prepare(query).asIterable()) { 60 | sum += (Long) e.getProperty("count"); 61 | } 62 | 63 | return sum; 64 | } 65 | 66 | /** 67 | * Increment the value of this sharded counter. 68 | */ 69 | public void increment() { 70 | final int shardNum = generator.nextInt(NUM_SHARDS); 71 | final Key shardKey = KeyFactory.createKey("SimpleCounterShard", 72 | Integer.toString(shardNum)); 73 | 74 | final Transaction tx = ds.beginTransaction(); 75 | Entity shard; 76 | try { 77 | shard = ds.get(tx, shardKey); 78 | final long count = (Long) shard.getProperty("count"); 79 | shard.setUnindexedProperty("count", count + 1L); 80 | } catch (EntityNotFoundException e) { 81 | shard = new Entity(shardKey); 82 | shard.setUnindexedProperty("count", 1L); 83 | } 84 | ds.put(tx, shard); 85 | tx.commit(); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v2/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v2; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Renders an HTML page showing current count of the sharded counter. 25 | * 26 | * Through the form, the user can increment the counter or add new shards. 27 | */ 28 | public class CounterPage extends HttpServlet { 29 | 30 | @Override 31 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 32 | throws IOException { 33 | displayPageStart(resp); 34 | final ShardedCounter counter = new ShardedCounter("test-counter"); 35 | displayCounts(counter, resp); 36 | displayInputFormAndClose(resp); 37 | } 38 | 39 | @Override 40 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 41 | throws IOException { 42 | displayPageStart(resp); 43 | final ShardedCounter counter = new ShardedCounter("test-counter"); 44 | if (Boolean.parseBoolean(req.getParameter("addShard"))) { 45 | final int inc = Integer.parseInt(req.getParameter("inc")); 46 | displayCount(counter, resp); 47 | displayNumberOfShards(counter.addShards(inc), resp); 48 | } else { 49 | counter.increment(); 50 | displayCount(counter, resp); 51 | displayNumberOfShards(counter.getShardCount(), resp); 52 | } 53 | displayInputFormAndClose(resp); 54 | } 55 | 56 | private void displayPageStart(HttpServletResponse resp) throws IOException { 57 | resp.getWriter().println(""); 58 | resp.getWriter().println(" "); 59 | } 60 | 61 | private void displayCount(ShardedCounter counter, HttpServletResponse resp) 62 | throws IOException { 63 | resp.getWriter() 64 | .println("Current count: " + counter.getCount() + "
"); 65 | } 66 | 67 | private void displayCounts(ShardedCounter counter, HttpServletResponse resp) 68 | throws IOException { 69 | displayCount(counter, resp); 70 | displayNumberOfShards(counter.getShardCount(), resp); 71 | } 72 | 73 | private void displayNumberOfShards(long shards, HttpServletResponse resp) 74 | throws IOException { 75 | resp.getWriter().print("Counter has " + shards); 76 | if (shards == 1) { 77 | resp.getWriter().println(" shard
"); 78 | } else { 79 | resp.getWriter().println(" shards"); 80 | } 81 | } 82 | 83 | private void displayInputFormAndClose(HttpServletResponse resp) 84 | throws IOException { 85 | resp.getWriter().println(""); 88 | 89 | resp.getWriter().println(""); 96 | 97 | resp.getWriter().println(" "); 98 | resp.getWriter().println(""); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/java/v2/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2011 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.java.v2; 17 | 18 | import com.google.appengine.api.datastore.DatastoreService; 19 | import com.google.appengine.api.datastore.DatastoreServiceFactory; 20 | import com.google.appengine.api.datastore.Entity; 21 | import com.google.appengine.api.datastore.EntityNotFoundException; 22 | import com.google.appengine.api.datastore.Key; 23 | import com.google.appengine.api.datastore.KeyFactory; 24 | import com.google.appengine.api.datastore.Query; 25 | import com.google.appengine.api.datastore.Transaction; 26 | import com.google.appengine.api.memcache.Expiration; 27 | import com.google.appengine.api.memcache.MemcacheService; 28 | import com.google.appengine.api.memcache.MemcacheService.SetPolicy; 29 | import com.google.appengine.api.memcache.MemcacheServiceFactory; 30 | 31 | import java.util.Random; 32 | 33 | /** 34 | * A counter which can be incremented rapidly. 35 | * 36 | * Capable of incrementing the counter and increasing the number of shards. When 37 | * incrementing, a random shard is selected to prevent a single shard from being 38 | * written too frequently. If increments are being made too quickly, increase 39 | * the number of shards to divide the load. Performs datastore operations using 40 | * the low level datastore API. 41 | */ 42 | public class ShardedCounter { 43 | 44 | /** 45 | * Convenience class which contains constants related to a named sharded 46 | * counter. The counter name provided in the constructor is used as 47 | * the entity key. 48 | */ 49 | private static final class Counter { 50 | /** 51 | * Entity kind representing a named sharded counter. 52 | */ 53 | private static final String KIND = "Counter"; 54 | 55 | /** 56 | * Property to store the number of shards in a given {@value #KIND} named 57 | * sharded counter. 58 | */ 59 | private static final String SHARD_COUNT = "shard_count"; 60 | } 61 | 62 | /** 63 | * Convenience class which contains constants related to the counter shards. 64 | * The shard number (as a String) is used as the entity key. 65 | */ 66 | private static final class CounterShard { 67 | /** 68 | * Entity kind prefix, which is concatenated with the counter name to form 69 | * the final entity kind, which represents counter shards. 70 | */ 71 | private static final String KIND_PREFIX = "CounterShard_"; 72 | 73 | /** 74 | * Property to store the current count within a counter shard. 75 | */ 76 | private static final String COUNT = "count"; 77 | } 78 | 79 | private static final DatastoreService ds = DatastoreServiceFactory 80 | .getDatastoreService(); 81 | 82 | /** 83 | * Default number of shards. 84 | */ 85 | private static final int INITIAL_SHARDS = 5; 86 | 87 | /** 88 | * The name of this counter. 89 | */ 90 | private final String counterName; 91 | 92 | /** 93 | * A random number generating, for distributing writes across shards. 94 | */ 95 | private final Random generator = new Random(); 96 | 97 | /** 98 | * The counter shard kind for this counter. 99 | */ 100 | private final String kind; 101 | 102 | private final MemcacheService mc = MemcacheServiceFactory 103 | .getMemcacheService(); 104 | 105 | /** 106 | * Constructor which creates a sharded counter using the provided counter 107 | * name. 108 | * 109 | * @param counterName name of the sharded counter 110 | */ 111 | public ShardedCounter(String counterName) { 112 | this.counterName = counterName; 113 | kind = CounterShard.KIND_PREFIX + counterName; 114 | } 115 | 116 | /** 117 | * Increase the number of shards for a given sharded counter. Will never 118 | * decrease the number of shards. 119 | * 120 | * @param count Number of new shards to build and store 121 | * @return the new number of shards 122 | */ 123 | public long addShards(int count) { 124 | final Key counterKey = KeyFactory.createKey(Counter.KIND, counterName); 125 | return incrementPropertyTx(counterKey, Counter.SHARD_COUNT, count, 126 | INITIAL_SHARDS + count); 127 | } 128 | 129 | /** 130 | * Retrieve the value of this sharded counter. 131 | * 132 | * @return Summed total of all shards' counts 133 | */ 134 | public long getCount() { 135 | final Long value = (Long) mc.get(kind); 136 | if (value != null) { 137 | return value; 138 | } 139 | 140 | long sum = 0; 141 | final Query query = new Query(kind); 142 | for (final Entity shard : ds.prepare(query).asIterable()) { 143 | sum += (Long) shard.getProperty(CounterShard.COUNT); 144 | } 145 | mc.put(kind, sum, Expiration.byDeltaSeconds(60), 146 | SetPolicy.ADD_ONLY_IF_NOT_PRESENT); 147 | 148 | return sum; 149 | } 150 | 151 | /** 152 | * Increment the value of this sharded counter. 153 | */ 154 | public void increment() { 155 | final int numShards = getShardCount(); 156 | 157 | final long shardNum = generator.nextInt(numShards); 158 | 159 | final Key shardKey = KeyFactory.createKey(kind, Long.toString(shardNum)); 160 | incrementPropertyTx(shardKey, CounterShard.COUNT, 1, 1); 161 | mc.increment(kind, 1); 162 | } 163 | 164 | /** 165 | * Get the number of shards in this counter. 166 | * 167 | * @return shard count 168 | */ 169 | public int getShardCount() { 170 | try { 171 | final Key counterKey = KeyFactory.createKey(Counter.KIND, counterName); 172 | final Entity counter = ds.get(counterKey); 173 | final Long shardCount = (Long) counter.getProperty(Counter.SHARD_COUNT); 174 | return shardCount.intValue(); 175 | } catch (EntityNotFoundException ignore) { 176 | return INITIAL_SHARDS; 177 | } 178 | } 179 | 180 | /** 181 | * Increment datastore property value inside a transaction. If the entity with 182 | * the provided key does not exist, instead create an entity with the supplied 183 | * initial property value. 184 | * 185 | * @param key the entity key to update or create 186 | * @param prop the property name to be incremented 187 | * @param increment the amount by which to increment 188 | * @param initialValue the value to use if the entity does not exist 189 | * @return the new value 190 | */ 191 | private long incrementPropertyTx(Key key, String prop, long increment, 192 | long initialValue) { 193 | final Transaction tx = ds.beginTransaction(); 194 | Entity thing; 195 | long value; 196 | try { 197 | thing = ds.get(tx, key); 198 | value = (Long) thing.getProperty(prop) + increment; 199 | } catch (EntityNotFoundException e) { 200 | thing = new Entity(key); 201 | value = initialValue; 202 | } 203 | thing.setUnindexedProperty(prop, value); 204 | ds.put(tx, thing); 205 | tx.commit(); 206 | return value; 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/PMF.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo; 17 | 18 | import javax.jdo.JDOHelper; 19 | import javax.jdo.PersistenceManagerFactory; 20 | 21 | public final class PMF { 22 | private static final PersistenceManagerFactory pmfInstance = 23 | JDOHelper.getPersistenceManagerFactory("transactions-optional"); 24 | 25 | private PMF() {} 26 | 27 | public static PersistenceManagerFactory get() { 28 | return pmfInstance; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v1/Counter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v1; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import java.util.List; 21 | import javax.jdo.PersistenceManager; 22 | 23 | /** 24 | * This initial implementation simply counts all instances of the 25 | * LimitedCounterShard class in the datastore. The only way to increment the 26 | * counter is to add another shard (creating another entity in the datastore). 27 | * This is not the correct approach, since we're limited to the number of 28 | * objects we can fetch with the query, but this is a simple foundation showing 29 | * how to add a new shard. 30 | * 31 | */ 32 | public class Counter { 33 | 34 | public Counter() { 35 | } 36 | 37 | public int getCount() { 38 | int sum = 0; 39 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 40 | ListCurrent count: " + counter.getCount() 37 | + "
"); 38 | 39 | resp.getWriter().println(""); 43 | 44 | resp.getWriter().println(" "); 45 | resp.getWriter().println(""); 46 | } 47 | 48 | @Override 49 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 50 | throws IOException { 51 | final Counter counter = new Counter(); 52 | counter.addShard(); 53 | doGet(req, resp); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v1/LimitedCounterShard.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v1; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * A JDO object representing a single shard belonging to the counter. 26 | * 27 | */ 28 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 29 | public class LimitedCounterShard { 30 | @PrimaryKey 31 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 32 | private Long id; 33 | 34 | @Persistent 35 | private Integer count; 36 | 37 | public LimitedCounterShard() { 38 | this.count = Integer.valueOf(0); 39 | } 40 | 41 | public LimitedCounterShard(Integer count) { 42 | this.count = count; 43 | } 44 | 45 | public Long getId() { 46 | return id; 47 | } 48 | 49 | public Integer getCount() { 50 | return count; 51 | } 52 | 53 | public void setCount(Integer count) { 54 | this.count = count; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/Counter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * Represents a counter in the datastore and stores the number of shards. 26 | * 27 | */ 28 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 29 | public class Counter { 30 | @PrimaryKey 31 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 32 | private Long id; 33 | 34 | @Persistent 35 | private final String counterName; 36 | 37 | @Persistent 38 | private Integer numShards; 39 | 40 | public Counter(String counterName) { 41 | this.counterName = counterName; 42 | this.numShards = Integer.valueOf(0); 43 | } 44 | 45 | public Counter(String counterName, Integer numShards) { 46 | this.counterName = counterName; 47 | this.numShards = numShards; 48 | } 49 | 50 | public Long getId() { 51 | return id; 52 | } 53 | 54 | public String getCounterName() { 55 | return counterName; 56 | } 57 | 58 | public Integer getShardCount() { 59 | return numShards; 60 | } 61 | 62 | public void setShardCount(int count) { 63 | this.numShards = Integer.valueOf(count); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/CounterFactory.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import javax.jdo.PersistenceManager; 21 | 22 | /** 23 | * Finds or creates a sharded counter with the desired name. 24 | * 25 | */ 26 | public class CounterFactory { 27 | 28 | public ShardedCounter getCounter(String name) { 29 | final ShardedCounter counter = new ShardedCounter(name); 30 | if (counter.isInDatastore()) { 31 | return counter; 32 | } else { 33 | return null; 34 | } 35 | } 36 | 37 | public ShardedCounter createCounter(String name) { 38 | final ShardedCounter counter = new ShardedCounter(name); 39 | 40 | final Counter counterEntity = new Counter(name, 0); 41 | final PersistenceManager pm = PMF.get().getPersistenceManager(); 42 | try { 43 | pm.makePersistent(counterEntity); 44 | } finally { 45 | pm.close(); 46 | } 47 | 48 | return counter; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/CounterPage.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import java.io.IOException; 19 | import javax.servlet.http.HttpServlet; 20 | import javax.servlet.http.HttpServletRequest; 21 | import javax.servlet.http.HttpServletResponse; 22 | 23 | /** 24 | * Renders an HTML page showing current count of the sharded counter. 25 | * 26 | * Through the form, the user can increment the counter or add new shards. 27 | * 28 | */ 29 | public class CounterPage extends HttpServlet { 30 | 31 | @Override 32 | public void doGet(HttpServletRequest req, HttpServletResponse resp) 33 | throws IOException { 34 | displayPageStart(resp); 35 | final ShardedCounter counter = getOrCreateCounter(resp); 36 | displayCounts(counter, resp); 37 | displayInputFormAndClose(resp); 38 | } 39 | 40 | @Override 41 | public void doPost(HttpServletRequest req, HttpServletResponse resp) 42 | throws IOException { 43 | displayPageStart(resp); 44 | final ShardedCounter counter = getOrCreateCounter(resp); 45 | if (Boolean.parseBoolean(req.getParameter("addShard"))) { 46 | displayCount(counter, resp); 47 | displayNumberOfShards(counter.addShard(), resp); 48 | } else { 49 | counter.increment(); 50 | displayCount(counter, resp); 51 | displayNumberOfShards(counter.getNumShards(), resp); 52 | } 53 | displayInputFormAndClose(resp); 54 | } 55 | 56 | private void displayPageStart(HttpServletResponse resp) throws IOException { 57 | resp.getWriter().println(""); 58 | resp.getWriter().println(" "); 59 | } 60 | 61 | /** 62 | * Creates the sharded counter if it does not yet exist. 63 | */ 64 | private ShardedCounter getOrCreateCounter(HttpServletResponse resp) 65 | throws IOException { 66 | final CounterFactory factory = new CounterFactory(); 67 | ShardedCounter counter = factory.getCounter("test-counter"); 68 | if (counter == null) { 69 | counter = factory.createCounter("test-counter"); 70 | counter.addShard(); 71 | resp.getWriter().println( 72 | "No counter named 'test-counter', so we created one.
"); 73 | } 74 | return counter; 75 | } 76 | 77 | private void displayCount(ShardedCounter counter, HttpServletResponse resp) 78 | throws IOException { 79 | resp.getWriter().println( 80 | "Current count: " + counter.getCount() + "
"); 81 | } 82 | 83 | private void displayCounts(ShardedCounter counter, HttpServletResponse resp) 84 | throws IOException { 85 | displayCount(counter, resp); 86 | displayNumberOfShards(counter.getNumShards(), resp); 87 | } 88 | 89 | private void displayNumberOfShards(int shards, HttpServletResponse resp) 90 | throws IOException { 91 | resp.getWriter().print("Counter has " + shards); 92 | if (shards == 1) { 93 | resp.getWriter().println(" shard
"); 94 | } else { 95 | resp.getWriter().println(" shards"); 96 | } 97 | } 98 | 99 | private void displayInputFormAndClose(HttpServletResponse resp) 100 | throws IOException { 101 | resp.getWriter().println(""); 104 | 105 | resp.getWriter().println(""); 111 | 112 | resp.getWriter().println(" "); 113 | resp.getWriter().println(""); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/CounterShard.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * One shard belonging to the named counter. 26 | * 27 | * An individual shard is written to infrequently to allow the counter in 28 | * aggregate to be incremented rapidly. 29 | * 30 | */ 31 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 32 | public class CounterShard { 33 | @PrimaryKey 34 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 35 | private Long id; 36 | 37 | @Persistent 38 | private final Integer shardNumber; 39 | 40 | @Persistent 41 | private final String counterName; 42 | 43 | @Persistent 44 | private Integer count; 45 | 46 | public CounterShard(String counterName, int shardNumber) { 47 | this(counterName, shardNumber, 0); 48 | } 49 | 50 | public CounterShard(String counterName, int shardNumber, int count) { 51 | this.counterName = counterName; 52 | this.shardNumber = Integer.valueOf(shardNumber); 53 | this.count = Integer.valueOf(count); 54 | } 55 | 56 | public Long getId() { 57 | return id; 58 | } 59 | 60 | public String getCounterName() { 61 | return counterName; 62 | } 63 | 64 | public Integer getShardNumber() { 65 | return shardNumber; 66 | } 67 | 68 | public Integer getCount() { 69 | return count; 70 | } 71 | 72 | public void setCount(Integer count) { 73 | this.count = count; 74 | } 75 | 76 | public void increment(int amount) { 77 | count = Integer.valueOf(count.intValue() + amount); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v2/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v2; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import java.util.List; 21 | import java.util.Random; 22 | import javax.jdo.PersistenceManager; 23 | import javax.jdo.Query; 24 | 25 | /** 26 | * A counter which can be incremented rapidly. 27 | * 28 | * Capable of incrementing the counter and increasing the number of shards. 29 | * When incrementing, a random shard is selected to prevent a single shard 30 | * from being written to too frequently. If increments are being made too 31 | * quickly, increase the number of shards to divide the load. Performs 32 | * datastore operations using JDO. 33 | * 34 | */ 35 | public class ShardedCounter { 36 | private final String counterName; 37 | 38 | public ShardedCounter(String counterName) { 39 | this.counterName = counterName; 40 | } 41 | 42 | public String getCounterName() { 43 | return counterName; 44 | } 45 | 46 | private Counter getThisCounter(PersistenceManager pm) { 47 | Counter current = null; 48 | final Query thisCounterQuery = pm.newQuery(Counter.class, 49 | "counterName == nameParam"); 50 | thisCounterQuery.declareParameters("String nameParam"); 51 | final ListNo counter named 'test-counter', so we created one.
"); 73 | } 74 | return counter; 75 | } 76 | 77 | private void displayCount(ShardedCounter counter, HttpServletResponse resp) 78 | throws IOException { 79 | resp.getWriter() 80 | .println("Current count: " + counter.getCount() + "
"); 81 | } 82 | 83 | private void displayCounts(ShardedCounter counter, HttpServletResponse resp) 84 | throws IOException { 85 | displayCount(counter, resp); 86 | displayNumberOfShards(counter.getNumShards(), resp); 87 | } 88 | 89 | private void displayNumberOfShards(int shards, HttpServletResponse resp) 90 | throws IOException { 91 | resp.getWriter().print("Counter has " + shards); 92 | if (shards == 1) { 93 | resp.getWriter().println(" shard
"); 94 | } else { 95 | resp.getWriter().println(" shards"); 96 | } 97 | } 98 | 99 | private void displayInputFormAndClose(HttpServletResponse resp) 100 | throws IOException { 101 | resp.getWriter().println(""); 104 | 105 | resp.getWriter().println(""); 111 | 112 | resp.getWriter().println(" "); 113 | resp.getWriter().println(""); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/DatastoreCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * Represents a counter in the datastore and stores the number of shards. 26 | * 27 | */ 28 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 29 | public class DatastoreCounter { 30 | @PrimaryKey 31 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 32 | private Long id; 33 | 34 | @Persistent 35 | private final String counterName; 36 | 37 | @Persistent 38 | private Integer numShards; 39 | 40 | public DatastoreCounter(String counterName) { 41 | this.counterName = counterName; 42 | this.numShards = Integer.valueOf(0); 43 | } 44 | 45 | public DatastoreCounter(String counterName, Integer numShards) { 46 | this.counterName = counterName; 47 | this.numShards = numShards; 48 | } 49 | 50 | public Long getId() { 51 | return id; 52 | } 53 | 54 | public String getCounterName() { 55 | return counterName; 56 | } 57 | 58 | public Integer getShardCount() { 59 | return numShards; 60 | } 61 | 62 | public void setShardCount(int count) { 63 | this.numShards = Integer.valueOf(count); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/DatastoreCounterShard.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import javax.jdo.annotations.IdGeneratorStrategy; 19 | import javax.jdo.annotations.IdentityType; 20 | import javax.jdo.annotations.PersistenceCapable; 21 | import javax.jdo.annotations.Persistent; 22 | import javax.jdo.annotations.PrimaryKey; 23 | 24 | /** 25 | * One shard belonging to the named counter. 26 | * 27 | * An individual shard is written to infrequently to allow the counter in 28 | * aggregate to be incremented rapidly. 29 | * 30 | */ 31 | @PersistenceCapable(identityType = IdentityType.APPLICATION) 32 | public class DatastoreCounterShard { 33 | @PrimaryKey 34 | @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 35 | private Long id; 36 | 37 | @Persistent 38 | private final Integer shardNumber; 39 | 40 | @Persistent 41 | private final String counterName; 42 | 43 | @Persistent 44 | private Integer count; 45 | 46 | public DatastoreCounterShard(String counterName, int shardNumber) { 47 | this(counterName, shardNumber, 0); 48 | } 49 | 50 | public DatastoreCounterShard(String counterName, int shardNumber, 51 | int count) { 52 | this.counterName = counterName; 53 | this.shardNumber = Integer.valueOf(shardNumber); 54 | this.count = Integer.valueOf(count); 55 | } 56 | 57 | public Long getId() { 58 | return id; 59 | } 60 | 61 | public String getCounterName() { 62 | return counterName; 63 | } 64 | 65 | public Integer getShardNumber() { 66 | return shardNumber; 67 | } 68 | 69 | public Integer getCount() { 70 | return count; 71 | } 72 | 73 | public void setCount(Integer count) { 74 | this.count = count; 75 | } 76 | 77 | public void increment(int amount) { 78 | count = Integer.valueOf(count.intValue() + amount); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /appengine-modules-shardedcounter/src/main/java/com/google/appengine/demos/shardedcounter/jdo/v3/ShardedCounter.java: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2009 Google Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | package com.google.appengine.demos.shardedcounter.jdo.v3; 17 | 18 | import com.google.appengine.demos.shardedcounter.jdo.PMF; 19 | 20 | import java.util.Collections; 21 | import java.util.List; 22 | import java.util.Random; 23 | 24 | import net.sf.jsr107cache.Cache; 25 | import net.sf.jsr107cache.CacheException; 26 | import net.sf.jsr107cache.CacheManager; 27 | import javax.jdo.PersistenceManager; 28 | import javax.jdo.Query; 29 | 30 | /** 31 | * A counter which can be incremented rapidly. 32 | * 33 | * Capable of incrementing the counter and increasing the number of shards. 34 | * When incrementing, a random shard is selected to prevent a single shard 35 | * from being written to too frequently. If increments are being made too 36 | * quickly, increase the number of shards to divide the load. Performs 37 | * datastore operations using JDO. 38 | * 39 | * Lookups are attempted using Memcache (Jcache). If the counter value is 40 | * not in the cache, the shards are read from the datastore and accumulated 41 | * to reconstruct the current count. 42 | * 43 | */ 44 | public class ShardedCounter { 45 | private final String counterName; 46 | private Cache cache; 47 | 48 | public ShardedCounter(String counterName) { 49 | this.counterName = counterName; 50 | cache = null; 51 | try { 52 | cache = CacheManager.getInstance().getCacheFactory().createCache( 53 | Collections.emptyMap()); 54 | } catch (CacheException e) { 55 | } 56 | } 57 | 58 | public String getCounterName() { 59 | return counterName; 60 | } 61 | 62 | private DatastoreCounter getThisCounter(PersistenceManager pm) { 63 | DatastoreCounter current = null; 64 | final Query thisCounterQuery = pm.newQuery(DatastoreCounter.class, 65 | "counterName == nameParam"); 66 | thisCounterQuery.declareParameters("String nameParam"); 67 | final List