1 /* Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 /*
24 Key cache assignments
25 */
26
27 #include "myisamdef.h"
28
29 /*
30 Assign pages of the index file for a table to a key cache
31
32 SYNOPSIS
33 mi_assign_to_key_cache()
34 info open table
35 key_map map of indexes to assign to the key cache
36 key_cache_ptr pointer to the key cache handle
37 assign_lock Mutex to lock during assignment
38
39 PREREQUESTS
40 One must have a READ lock or a WRITE lock on the table when calling
41 the function to ensure that there is no other writers to it.
42
43 The caller must also ensure that one doesn't call this function from
44 two different threads with the same table.
45
46 NOTES
47 At present pages for all indexes must be assigned to the same key cache.
48 In future only pages for indexes specified in the key_map parameter
49 of the table will be assigned to the specified key cache.
50
51 RETURN VALUE
52 0 If a success
53 # Error code
54 */
55
mi_assign_to_key_cache(MI_INFO * info,ulonglong key_map MY_ATTRIBUTE ((unused)),KEY_CACHE * key_cache)56 int mi_assign_to_key_cache(MI_INFO *info,
57 ulonglong key_map MY_ATTRIBUTE((unused)),
58 KEY_CACHE *key_cache)
59 {
60 int error= 0;
61 MYISAM_SHARE* share= info->s;
62 DBUG_ENTER("mi_assign_to_key_cache");
63 DBUG_PRINT("enter",("old_key_cache_handle: 0x%lx new_key_cache_handle: 0x%lx",
64 (long) share->key_cache, (long) key_cache));
65
66 /*
67 Skip operation if we didn't change key cache. This can happen if we
68 call this for all open instances of the same table
69 */
70 if (share->key_cache == key_cache)
71 DBUG_RETURN(0);
72
73 /*
74 First flush all blocks for the table in the old key cache.
75 This is to ensure that the disk is consistent with the data pages
76 in memory (which may not be the case if the table uses delayed_key_write)
77
78 Note that some other read thread may still fill in the key cache with
79 new blocks during this call and after, but this doesn't matter as
80 all threads will start using the new key cache for their next call to
81 myisam library and we know that there will not be any changed blocks
82 in the old key cache.
83 */
84
85 if (flush_key_blocks(share->key_cache, keycache_thread_var(),
86 share->kfile, FLUSH_RELEASE))
87 {
88 error= my_errno();
89 mi_print_error(info->s, HA_ERR_CRASHED);
90 mi_mark_crashed(info); /* Mark that table must be checked */
91 }
92
93 /*
94 Flush the new key cache for this file. This is needed to ensure
95 that there is no old blocks (with outdated data) left in the new key
96 cache from an earlier assign_to_keycache operation
97
98 (This can never fail as there is never any not written data in the
99 new key cache)
100 */
101 (void) flush_key_blocks(key_cache, keycache_thread_var(),
102 share->kfile, FLUSH_RELEASE);
103
104 /*
105 ensure that setting the key cache and changing the multi_key_cache
106 is done atomicly
107 */
108 mysql_mutex_lock(&share->intern_lock);
109 /*
110 Tell all threads to use the new key cache
111 This should be seen at the lastes for the next call to an myisam function.
112 */
113 share->key_cache= key_cache;
114
115 /* store the key cache in the global hash structure for future opens */
116 if (multi_key_cache_set((uchar*) share->unique_file_name,
117 share->unique_name_length,
118 share->key_cache))
119 error= my_errno();
120 mysql_mutex_unlock(&share->intern_lock);
121 DBUG_RETURN(error);
122 }
123
124
125 /*
126 Change all MyISAM entries that uses one key cache to another key cache
127
128 SYNOPSIS
129 mi_change_key_cache()
130 old_key_cache Old key cache
131 new_key_cache New key cache
132
133 NOTES
134 This is used when we delete one key cache.
135
136 To handle the case where some other threads tries to open an MyISAM
137 table associated with the to-be-deleted key cache while this operation
138 is running, we have to call 'multi_key_cache_change()' from this
139 function while we have a lock on the MyISAM table list structure.
140
141 This is safe as long as it's only MyISAM that is using this specific
142 key cache.
143 */
144
145
mi_change_key_cache(KEY_CACHE * old_key_cache,KEY_CACHE * new_key_cache)146 void mi_change_key_cache(KEY_CACHE *old_key_cache,
147 KEY_CACHE *new_key_cache)
148 {
149 LIST *pos;
150 DBUG_ENTER("mi_change_key_cache");
151
152 /*
153 Lock list to ensure that no one can close the table while we manipulate it
154 */
155 mysql_mutex_lock(&THR_LOCK_myisam);
156 for (pos=myisam_open_list ; pos ; pos=pos->next)
157 {
158 MI_INFO *info= (MI_INFO*) pos->data;
159 MYISAM_SHARE *share= info->s;
160 if (share->key_cache == old_key_cache)
161 mi_assign_to_key_cache(info, (ulonglong) ~0, new_key_cache);
162 }
163
164 /*
165 We have to do the following call while we have the lock on the
166 MyISAM list structure to ensure that another thread is not trying to
167 open a new table that will be associted with the old key cache
168 */
169 multi_key_cache_change(old_key_cache, new_key_cache);
170 mysql_mutex_unlock(&THR_LOCK_myisam);
171 DBUG_VOID_RETURN;
172 }
173