File: T_CacheUser.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (195 lines) | stat: -rw-r--r-- 4,736 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*

   Derby - Class org.apache.derbyTesting.unitTests.services.T_CacheUser

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.unitTests.services;

import org.apache.derby.iapi.services.cache.*;

import org.apache.derbyTesting.unitTests.harness.T_Fail;

import org.apache.derby.iapi.services.stream.HeaderPrintWriter;

import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.reference.SQLState;

public class T_CacheUser implements Runnable {

	protected CacheManager	cm;
	protected int           iterations;
	protected HeaderPrintWriter out;
	protected T_CacheService parent;

	public T_CacheUser(CacheManager cm, int iterations, T_CacheService parent, HeaderPrintWriter out) {
		this.cm = cm;
		this.iterations = iterations;
		this.parent = parent;
		this.out = out;
	}


	public void run() {
		try {
			thrashCache();
		} catch (T_Fail tf) {
			parent.setChildException(tf);
		} catch (StandardException se) {
			parent.setChildException(T_Fail.exceptionFail(se));
		}
	}
	/**
		T_CachedInteger range - 0 - 100

		pick a key randomly
					48%/48%/4% chance of Int/String/invalid key
					90%/5%/5% chance of can find / can't find / raise exception
					50%/30%/20% find/findCached/create
					

		@exception StandardException  Standard Derby Error policy
		@exception T_Fail  Some error
				
		
	*/

	public void thrashCache() throws StandardException, T_Fail {

		// stats
		int f = 0, fs = 0, ff = 0, fe = 0;
		int fc = 0, fcs = 0, fcf = 0;
		int c = 0, cs = 0, cf = 0, ce = 0, cse = 0;
		int cleanAll = 0, ageOut = 0;
		int release = 0, remove = 0;


		for (int i = 0; i < iterations; i++) {

			if ((i % 100) == 0)
				out.printlnWithHeader("iteration " + i);

			T_Key tkey = T_Key.randomKey();

			double rand = Math.random();
			T_Cacheable e = null;
			if (rand < 0.5) {
				f++;

				try {

					e = (T_Cacheable) cm.find(tkey);
					if (e == null) {
						ff++;
						continue;
					}

					fs++;

				} catch (T_CacheException tc) {
					if (tc.getType() == T_CacheException.ERROR)
						throw tc;

					// acceptable error
					fe++;
					continue;
				}			
			} else if (rand < 0.8)  {

				fc++;

				e = (T_Cacheable) cm.findCached(tkey);
				if (e == null) {
					fcf++;
					continue;
				}
				fcs++;

			} else {
				c++;

				try {

					e = (T_Cacheable) cm.create(tkey, Thread.currentThread());
					if (e == null) {
						cf++;
						continue;
					}

					cs++;
 
				} catch (T_CacheException tc) {
					if (tc.getType() == T_CacheException.ERROR)
						throw tc;

					// acceptable error
					ce++;
					continue;
				} catch (StandardException se) {

					if (se.getMessageId().equals(SQLState.OBJECT_EXISTS_IN_CACHE)) {
						cse++;
						continue;
					}
					throw se;
				}			
			}

			// ensure we can find it cached and that the key matches
			cm.release(parent.t_findCachedSucceed(cm, tkey));

			if (Math.random() < 0.25)
				e.setDirty();

			if (Math.random() < 0.75)
				Thread.yield();

			if ((Math.random() < 0.10) && (e.canRemove())) {
				remove++;
				cm.remove(e);
			} else {
				release++;
				cm.release(e);
			}
			e = null;

			double rand2 = Math.random();
			
			if (rand2 < 0.02) {
				cleanAll++;
				cm.cleanAll();
			}
			else if (rand2 < 0.04) {
				ageOut++;
				cm.ageOut();
			}
		}

		// ensure all our output in grouped.
		synchronized (parent) {
			out.printlnWithHeader("find()       calls " + f  + " : found/not found/exception : " + fs + "/" + ff + "/" + fe);
			out.printlnWithHeader("findCached() calls " + fc + " : found/not found           : " + fcs + "/" + fcf);
			out.printlnWithHeader("create()     calls " + c  + " : found/not found/exception/standard exception : " + cs + "/" + cf + "/" + ce + "/" + cse);
			out.printlnWithHeader("release()    calls " + release);
			out.printlnWithHeader("remove()     calls " + remove);
			out.printlnWithHeader("cleanAll()   calls " + cleanAll);
			out.printlnWithHeader("ageOut()     calls " + ageOut);
		}

	}
}